github.com/gogf/gf/v2@v2.7.4/net/gclient/gclient_z_example_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gclient_test 8 9 import ( 10 "context" 11 "crypto/tls" 12 "encoding/hex" 13 "fmt" 14 "net/http" 15 "time" 16 17 "github.com/gogf/gf/v2/debug/gdebug" 18 "github.com/gogf/gf/v2/frame/g" 19 "github.com/gogf/gf/v2/net/gclient" 20 "github.com/gogf/gf/v2/net/ghttp" 21 "github.com/gogf/gf/v2/os/gctx" 22 "github.com/gogf/gf/v2/os/gfile" 23 "github.com/gogf/gf/v2/util/guid" 24 ) 25 26 var ( 27 crtFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/server.crt" 28 keyFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/server.key" 29 ) 30 31 func init() { 32 // Default server for client. 33 p := 8999 34 s := g.Server(p) 35 // HTTP method handlers. 36 s.Group("/", func(group *ghttp.RouterGroup) { 37 group.GET("/", func(r *ghttp.Request) { 38 r.Response.Writef( 39 "GET: query: %d, %s", 40 r.GetQuery("id").Int(), 41 r.GetQuery("name").String(), 42 ) 43 }) 44 group.PUT("/", func(r *ghttp.Request) { 45 r.Response.Writef( 46 "PUT: form: %d, %s", 47 r.GetForm("id").Int(), 48 r.GetForm("name").String(), 49 ) 50 }) 51 group.POST("/", func(r *ghttp.Request) { 52 r.Response.Writef( 53 "POST: form: %d, %s", 54 r.GetForm("id").Int(), 55 r.GetForm("name").String(), 56 ) 57 }) 58 group.DELETE("/", func(r *ghttp.Request) { 59 r.Response.Writef( 60 "DELETE: form: %d, %s", 61 r.GetForm("id").Int(), 62 r.GetForm("name").String(), 63 ) 64 }) 65 group.HEAD("/", func(r *ghttp.Request) { 66 r.Response.Writef( 67 "HEAD: form: %d, %s", 68 r.GetForm("id").Int(), 69 r.GetForm("name").String(), 70 ) 71 }) 72 group.PATCH("/", func(r *ghttp.Request) { 73 r.Response.Writef( 74 "PATCH: form: %d, %s", 75 r.GetForm("id").Int(), 76 r.GetForm("name").String(), 77 ) 78 }) 79 group.CONNECT("/", func(r *ghttp.Request) { 80 r.Response.Writef( 81 "CONNECT: form: %d, %s", 82 r.GetForm("id").Int(), 83 r.GetForm("name").String(), 84 ) 85 }) 86 group.OPTIONS("/", func(r *ghttp.Request) { 87 r.Response.Writef( 88 "OPTIONS: form: %d, %s", 89 r.GetForm("id").Int(), 90 r.GetForm("name").String(), 91 ) 92 }) 93 group.TRACE("/", func(r *ghttp.Request) { 94 r.Response.Writef( 95 "TRACE: form: %d, %s", 96 r.GetForm("id").Int(), 97 r.GetForm("name").String(), 98 ) 99 }) 100 }) 101 // Client chaining operations handlers. 102 s.Group("/", func(group *ghttp.RouterGroup) { 103 group.ALL("/header", func(r *ghttp.Request) { 104 r.Response.Writef( 105 "Span-Id: %s, Trace-Id: %s", 106 r.Header.Get("Span-Id"), 107 r.Header.Get("Trace-Id"), 108 ) 109 }) 110 group.ALL("/cookie", func(r *ghttp.Request) { 111 r.Response.Writef( 112 "SessionId: %s", 113 r.Cookie.Get("SessionId"), 114 ) 115 }) 116 group.ALL("/json", func(r *ghttp.Request) { 117 r.Response.Writef( 118 "Content-Type: %s, id: %d", 119 r.Header.Get("Content-Type"), 120 r.Get("id").Int(), 121 ) 122 }) 123 }) 124 // Other testing handlers. 125 s.Group("/var", func(group *ghttp.RouterGroup) { 126 group.ALL("/json", func(r *ghttp.Request) { 127 r.Response.Write(`{"id":1,"name":"john"}`) 128 }) 129 group.ALL("/jsons", func(r *ghttp.Request) { 130 r.Response.Write(`[{"id":1,"name":"john"}, {"id":2,"name":"smith"}]`) 131 }) 132 }) 133 s.SetAccessLogEnabled(false) 134 s.SetDumpRouterMap(false) 135 s.SetPort(p) 136 err := s.Start() 137 if err != nil { 138 panic(err) 139 } 140 time.Sleep(time.Millisecond * 500) 141 } 142 143 func ExampleNew() { 144 var ( 145 ctx = gctx.New() 146 client = gclient.New() 147 ) 148 149 if r, err := client.Get(ctx, "http://127.0.0.1:8999/var/json"); err != nil { 150 panic(err) 151 } else { 152 defer r.Close() 153 fmt.Println(r.ReadAllString()) 154 } 155 156 // Output: 157 // {"id":1,"name":"john"} 158 } 159 160 func ExampleClient_Clone() { 161 var ( 162 ctx = gctx.New() 163 client = gclient.New() 164 ) 165 166 client.SetCookie("key", "value") 167 cloneClient := client.Clone() 168 169 if r, err := cloneClient.Get(ctx, "http://127.0.0.1:8999/var/json"); err != nil { 170 panic(err) 171 } else { 172 defer r.Close() 173 fmt.Println(r.ReadAllString()) 174 } 175 176 // Output: 177 // {"id":1,"name":"john"} 178 } 179 180 func fromHex(s string) []byte { 181 b, _ := hex.DecodeString(s) 182 return b 183 } 184 185 func ExampleNew_MultiConn_Recommend() { 186 var ( 187 ctx = gctx.New() 188 client = g.Client() 189 ) 190 191 // controls the maximum idle(keep-alive) connections to keep per-host 192 client.Transport.(*http.Transport).MaxIdleConnsPerHost = 5 193 194 for i := 0; i < 5; i++ { 195 if r, err := client.Get(ctx, "http://127.0.0.1:8999/var/json"); err != nil { 196 panic(err) 197 } else { 198 fmt.Println(r.ReadAllString()) 199 r.Close() 200 } 201 } 202 203 // Output: 204 //{"id":1,"name":"john"} 205 //{"id":1,"name":"john"} 206 //{"id":1,"name":"john"} 207 //{"id":1,"name":"john"} 208 //{"id":1,"name":"john"} 209 } 210 211 func ExampleClient_Header() { 212 var ( 213 url = "http://127.0.0.1:8999/header" 214 header = g.MapStrStr{ 215 "Span-Id": "0.1", 216 "Trace-Id": "123456789", 217 } 218 ) 219 content := g.Client().Header(header).PostContent(ctx, url, g.Map{ 220 "id": 10000, 221 "name": "john", 222 }) 223 fmt.Println(content) 224 225 // Output: 226 // Span-Id: 0.1, Trace-Id: 123456789 227 } 228 229 func ExampleClient_HeaderRaw() { 230 var ( 231 url = "http://127.0.0.1:8999/header" 232 headerRaw = ` 233 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3950.0 Safari/537.36 234 Span-Id: 0.1 235 Trace-Id: 123456789 236 ` 237 ) 238 content := g.Client().HeaderRaw(headerRaw).PostContent(ctx, url, g.Map{ 239 "id": 10000, 240 "name": "john", 241 }) 242 fmt.Println(content) 243 244 // Output: 245 // Span-Id: 0.1, Trace-Id: 123456789 246 } 247 248 func ExampleClient_Cookie() { 249 var ( 250 url = "http://127.0.0.1:8999/cookie" 251 cookie = g.MapStrStr{ 252 "SessionId": "123", 253 } 254 ) 255 content := g.Client().Cookie(cookie).PostContent(ctx, url, g.Map{ 256 "id": 10000, 257 "name": "john", 258 }) 259 fmt.Println(content) 260 261 // Output: 262 // SessionId: 123 263 } 264 265 func ExampleClient_ContentJson() { 266 var ( 267 url = "http://127.0.0.1:8999/json" 268 jsonStr = `{"id":10000,"name":"john"}` 269 jsonMap = g.Map{ 270 "id": 10000, 271 "name": "john", 272 } 273 ) 274 // Post using JSON string. 275 fmt.Println(g.Client().ContentJson().PostContent(ctx, url, jsonStr)) 276 // Post using JSON map. 277 fmt.Println(g.Client().ContentJson().PostContent(ctx, url, jsonMap)) 278 279 // Output: 280 // Content-Type: application/json, id: 10000 281 // Content-Type: application/json, id: 10000 282 } 283 284 func ExampleClient_Post() { 285 url := "http://127.0.0.1:8999" 286 // Send with string parameter in request body. 287 r1, err := g.Client().Post(ctx, url, "id=10000&name=john") 288 if err != nil { 289 panic(err) 290 } 291 defer r1.Close() 292 fmt.Println(r1.ReadAllString()) 293 294 // Send with map parameter. 295 r2, err := g.Client().Post(ctx, url, g.Map{ 296 "id": 10000, 297 "name": "john", 298 }) 299 if err != nil { 300 panic(err) 301 } 302 defer r2.Close() 303 fmt.Println(r2.ReadAllString()) 304 305 // Output: 306 // POST: form: 10000, john 307 // POST: form: 10000, john 308 } 309 310 func ExampleClient_PostBytes() { 311 url := "http://127.0.0.1:8999" 312 fmt.Println(string(g.Client().PostBytes(ctx, url, g.Map{ 313 "id": 10000, 314 "name": "john", 315 }))) 316 317 // Output: 318 // POST: form: 10000, john 319 } 320 321 func ExampleClient_DeleteBytes() { 322 url := "http://127.0.0.1:8999" 323 fmt.Println(string(g.Client().DeleteBytes(ctx, url, g.Map{ 324 "id": 10000, 325 "name": "john", 326 }))) 327 328 // Output: 329 // DELETE: form: 10000, john 330 } 331 332 func ExampleClient_HeadBytes() { 333 url := "http://127.0.0.1:8999" 334 fmt.Println(string(g.Client().HeadBytes(ctx, url, g.Map{ 335 "id": 10000, 336 "name": "john", 337 }))) 338 339 // Output: 340 } 341 342 func ExampleClient_PatchBytes() { 343 url := "http://127.0.0.1:8999" 344 fmt.Println(string(g.Client().PatchBytes(ctx, url, g.Map{ 345 "id": 10000, 346 "name": "john", 347 }))) 348 349 // Output: 350 // PATCH: form: 10000, john 351 } 352 353 func ExampleClient_ConnectBytes() { 354 url := "http://127.0.0.1:8999" 355 fmt.Println(string(g.Client().ConnectBytes(ctx, url, g.Map{ 356 "id": 10000, 357 "name": "john", 358 }))) 359 360 // Output: 361 // CONNECT: form: 10000, john 362 } 363 364 func ExampleClient_OptionsBytes() { 365 url := "http://127.0.0.1:8999" 366 fmt.Println(string(g.Client().OptionsBytes(ctx, url, g.Map{ 367 "id": 10000, 368 "name": "john", 369 }))) 370 371 // Output: 372 // OPTIONS: form: 10000, john 373 } 374 375 func ExampleClient_TraceBytes() { 376 url := "http://127.0.0.1:8999" 377 fmt.Println(string(g.Client().TraceBytes(ctx, url, g.Map{ 378 "id": 10000, 379 "name": "john", 380 }))) 381 382 // Output: 383 // TRACE: form: 10000, john 384 } 385 386 func ExampleClient_PostContent() { 387 url := "http://127.0.0.1:8999" 388 fmt.Println(g.Client().PostContent(ctx, url, g.Map{ 389 "id": 10000, 390 "name": "john", 391 })) 392 393 // Output: 394 // POST: form: 10000, john 395 } 396 397 func ExampleClient_PostVar() { 398 type User struct { 399 Id int 400 Name string 401 } 402 var ( 403 users []User 404 url = "http://127.0.0.1:8999/var/jsons" 405 ) 406 err := g.Client().PostVar(ctx, url).Scan(&users) 407 if err != nil { 408 panic(err) 409 } 410 fmt.Println(users) 411 412 // Output: 413 // [{1 john} {2 smith}] 414 } 415 416 func ExampleClient_Get() { 417 var ( 418 ctx = context.Background() 419 url = "http://127.0.0.1:8999" 420 ) 421 422 // Send with string parameter along with URL. 423 r1, err := g.Client().Get(ctx, url+"?id=10000&name=john") 424 if err != nil { 425 panic(err) 426 } 427 defer r1.Close() 428 fmt.Println(r1.ReadAllString()) 429 430 // Send with string parameter in request body. 431 r2, err := g.Client().Get(ctx, url, "id=10000&name=john") 432 if err != nil { 433 panic(err) 434 } 435 defer r2.Close() 436 fmt.Println(r2.ReadAllString()) 437 438 // Send with map parameter. 439 r3, err := g.Client().Get(ctx, url, g.Map{ 440 "id": 10000, 441 "name": "john", 442 }) 443 if err != nil { 444 panic(err) 445 } 446 defer r3.Close() 447 fmt.Println(r3.ReadAllString()) 448 449 // Output: 450 // GET: query: 10000, john 451 // GET: query: 10000, john 452 // GET: query: 10000, john 453 } 454 455 func ExampleClient_Put() { 456 url := "http://127.0.0.1:8999" 457 r, _ := g.Client().Put(ctx, url, g.Map{ 458 "id": 10000, 459 "name": "john", 460 }) 461 defer r.Close() 462 fmt.Println(r.ReadAllString()) 463 464 // Output: 465 // PUT: form: 10000, john 466 } 467 468 func ExampleClient_GetBytes() { 469 var ( 470 ctx = context.Background() 471 url = "http://127.0.0.1:8999" 472 ) 473 fmt.Println(string(g.Client().GetBytes(ctx, url, g.Map{ 474 "id": 10000, 475 "name": "john", 476 }))) 477 478 // Output: 479 // GET: query: 10000, john 480 } 481 482 func ExampleClient_PutBytes() { 483 var ( 484 ctx = context.Background() 485 url = "http://127.0.0.1:8999" 486 ) 487 fmt.Println(string(g.Client().PutBytes(ctx, url, g.Map{ 488 "id": 10000, 489 "name": "john", 490 }))) 491 492 // Output: 493 // PUT: form: 10000, john 494 } 495 496 func ExampleClient_GetContent() { 497 url := "http://127.0.0.1:8999" 498 fmt.Println(g.Client().GetContent(ctx, url, g.Map{ 499 "id": 10000, 500 "name": "john", 501 })) 502 503 // Output: 504 // GET: query: 10000, john 505 } 506 507 func ExampleClient_GetVar() { 508 type User struct { 509 Id int 510 Name string 511 } 512 var ( 513 user *User 514 ctx = context.Background() 515 url = "http://127.0.0.1:8999/var/json" 516 ) 517 err := g.Client().GetVar(ctx, url).Scan(&user) 518 if err != nil { 519 panic(err) 520 } 521 fmt.Println(user) 522 523 // Output: 524 // &{1 john} 525 } 526 527 // ExampleClient_SetProxy an example for `gclient.Client.SetProxy` method. 528 // please prepare two proxy server before running this example. 529 // http proxy server listening on `127.0.0.1:1081` 530 // socks5 proxy server listening on `127.0.0.1:1080` 531 func ExampleClient_SetProxy() { 532 // connect to an http proxy server 533 client := g.Client() 534 client.SetProxy("http://127.0.0.1:1081") 535 client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout 536 resp, err := client.Get(ctx, "http://127.0.0.1:8999") 537 if err != nil { 538 // err is not nil when your proxy server is down. 539 // eg. Get "http://127.0.0.1:8999": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused 540 } 541 fmt.Println(err != nil) 542 resp.Close() 543 544 // connect to an http proxy server which needs auth 545 client.SetProxy("http://user:password:127.0.0.1:1081") 546 client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout 547 resp, err = client.Get(ctx, "http://127.0.0.1:8999") 548 if err != nil { 549 // err is not nil when your proxy server is down. 550 // eg. Get "http://127.0.0.1:8999": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused 551 } 552 fmt.Println(err != nil) 553 resp.Close() 554 555 // connect to a socks5 proxy server 556 client.SetProxy("socks5://127.0.0.1:1080") 557 client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout 558 resp, err = client.Get(ctx, "http://127.0.0.1:8999") 559 if err != nil { 560 // err is not nil when your proxy server is down. 561 // eg. Get "http://127.0.0.1:8999": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused 562 } 563 fmt.Println(err != nil) 564 resp.Close() 565 566 // connect to a socks5 proxy server which needs auth 567 client.SetProxy("socks5://user:password@127.0.0.1:1080") 568 client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout 569 resp, err = client.Get(ctx, "http://127.0.0.1:8999") 570 if err != nil { 571 // err is not nil when your proxy server is down. 572 // eg. Get "http://127.0.0.1:8999": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused 573 } 574 fmt.Println(err != nil) 575 resp.Close() 576 577 // Output: 578 // true 579 // true 580 // true 581 // true 582 } 583 584 // ExampleClient_Proxy a chain version of example for `gclient.Client.Proxy` method. 585 // please prepare two proxy server before running this example. 586 // http proxy server listening on `127.0.0.1:1081` 587 // socks5 proxy server listening on `127.0.0.1:1080` 588 // for more details, please refer to ExampleClient_SetProxy 589 func ExampleClient_Proxy() { 590 var ( 591 ctx = context.Background() 592 ) 593 client := g.Client() 594 _, err := client.Proxy("http://127.0.0.1:1081").Get(ctx, "http://127.0.0.1:8999") 595 fmt.Println(err != nil) 596 597 client2 := g.Client() 598 _, err = client2.Proxy("socks5://127.0.0.1:1080").Get(ctx, "http://127.0.0.1:8999") 599 fmt.Println(err != nil) 600 601 client3 := g.Client() 602 _, err = client3.Proxy("").Get(ctx, "http://127.0.0.1:8999") 603 fmt.Println(err != nil) 604 605 client4 := g.Client() 606 url := "http://127.0.0.1:1081" + string([]byte{0x7f}) 607 _, err = client4.Proxy(url).Get(ctx, "http://127.0.0.1:8999") 608 fmt.Println(err != nil) 609 610 // Output: 611 // true 612 // true 613 // false 614 // false 615 } 616 617 func ExampleClient_Prefix() { 618 var ( 619 ctx = gctx.New() 620 ) 621 622 s := g.Server(guid.S()) 623 // HTTP method handlers. 624 s.Group("/api", func(group *ghttp.RouterGroup) { 625 group.GET("/v1/prefix", func(r *ghttp.Request) { 626 r.Response.Write("this is v1 prefix") 627 }) 628 group.GET("/v1/hello", func(r *ghttp.Request) { 629 r.Response.Write("this is v1 hello") 630 }) 631 }) 632 s.SetAccessLogEnabled(false) 633 s.SetDumpRouterMap(false) 634 s.Start() 635 time.Sleep(time.Millisecond * 100) 636 637 // Add Client URI Prefix 638 client := g.Client().Prefix(fmt.Sprintf( 639 "http://127.0.0.1:%d/api/v1/", s.GetListenedPort(), 640 )) 641 642 fmt.Println(string(client.GetBytes(ctx, "prefix"))) 643 fmt.Println(string(client.GetBytes(ctx, "hello"))) 644 645 // Output: 646 // this is v1 prefix 647 // this is v1 hello 648 } 649 650 func ExampleClient_Retry() { 651 var ( 652 ctx = gctx.New() 653 url = "http://127.0.0.1:8999" 654 ) 655 client := g.Client().Retry(2, time.Second) 656 657 fmt.Println(string(client.GetBytes(ctx, url, g.Map{ 658 "id": 10000, 659 "name": "john", 660 }))) 661 662 // Output: 663 // GET: query: 10000, john 664 } 665 666 func ExampleClient_RedirectLimit() { 667 var ( 668 ctx = gctx.New() 669 url = "http://127.0.0.1:8999" 670 ) 671 client := g.Client().RedirectLimit(1) 672 673 fmt.Println(string(client.GetBytes(ctx, url, g.Map{ 674 "id": 10000, 675 "name": "john", 676 }))) 677 678 // Output: 679 // GET: query: 10000, john 680 } 681 682 func ExampleClient_SetBrowserMode() { 683 var ( 684 ctx = gctx.New() 685 url = "http://127.0.0.1:8999" 686 ) 687 client := g.Client().SetBrowserMode(true) 688 689 fmt.Println(string(client.GetBytes(ctx, url, g.Map{ 690 "id": 10000, 691 "name": "john", 692 }))) 693 694 // Output: 695 // GET: query: 10000, john 696 } 697 698 func ExampleClient_SetHeader() { 699 var ( 700 ctx = gctx.New() 701 url = "http://127.0.0.1:8999" 702 ) 703 client := g.Client() 704 client.SetHeader("Server", "GoFrameServer") 705 client.SetHeader("Client", "g.Client()") 706 707 fmt.Println(string(client.GetBytes(ctx, url, g.Map{ 708 "id": 10000, 709 "name": "john", 710 }))) 711 712 // Output: 713 // GET: query: 10000, john 714 } 715 716 func ExampleClient_SetRedirectLimit() { 717 go func() { 718 s := g.Server() 719 s.BindHandler("/hello", func(r *ghttp.Request) { 720 r.Response.Writeln("hello world") 721 }) 722 s.BindHandler("/back", func(r *ghttp.Request) { 723 r.Response.RedirectBack() 724 }) 725 s.SetDumpRouterMap(false) 726 s.SetPort(8199) 727 s.Run() 728 }() 729 730 time.Sleep(time.Second) 731 732 var ( 733 ctx = gctx.New() 734 urlHello = "http://127.0.0.1:8199/hello" 735 urlBack = "http://127.0.0.1:8199/back" 736 ) 737 client := g.Client().SetRedirectLimit(1) 738 client.SetHeader("Referer", urlHello) 739 740 resp, err := client.DoRequest(ctx, http.MethodGet, urlBack, g.Map{ 741 "id": 10000, 742 "name": "john", 743 }) 744 if err == nil { 745 fmt.Println(resp.ReadAllString()) 746 resp.Close() 747 } 748 749 client.SetRedirectLimit(2) 750 resp, err = client.DoRequest(ctx, http.MethodGet, urlBack, g.Map{ 751 "id": 10000, 752 "name": "john", 753 }) 754 if err == nil { 755 fmt.Println(resp.ReadAllString()) 756 resp.Close() 757 } 758 759 // Output: 760 // Found 761 // hello world 762 } 763 764 func ExampleClient_SetTLSKeyCrt() { 765 var ( 766 ctx = gctx.New() 767 url = "http://127.0.0.1:8999" 768 testCrtFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/upload/file1.txt" 769 testKeyFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/upload/file2.txt" 770 ) 771 client := g.Client() 772 client.SetTLSKeyCrt(testCrtFile, testKeyFile) 773 client.SetTLSKeyCrt(crtFile, keyFile) 774 fmt.Println(string(client.GetBytes(ctx, url, g.Map{ 775 "id": 10000, 776 "name": "john", 777 }))) 778 779 // Output: 780 // GET: query: 10000, john 781 } 782 783 func ExampleClient_SetTLSConfig() { 784 var ( 785 ctx = gctx.New() 786 url = "http://127.0.0.1:8999" 787 tlsConfig = &tls.Config{} 788 ) 789 client := g.Client() 790 client.SetTLSConfig(tlsConfig) 791 fmt.Println(string(client.GetBytes(ctx, url, g.Map{ 792 "id": 10000, 793 "name": "john", 794 }))) 795 796 // Output: 797 // GET: query: 10000, john 798 } 799 800 func ExampleClient_PutContent() { 801 url := "http://127.0.0.1:8999" 802 fmt.Println(g.Client().PutContent(ctx, url, g.Map{ 803 "id": 10000, 804 "name": "john", 805 })) 806 807 // Output: 808 // PUT: form: 10000, john 809 } 810 811 func ExampleClient_DeleteContent() { 812 url := "http://127.0.0.1:8999" 813 fmt.Println(g.Client().DeleteContent(ctx, url, g.Map{ 814 "id": 10000, 815 "name": "john", 816 })) 817 818 // Output: 819 // DELETE: form: 10000, john 820 } 821 822 func ExampleClient_HeadContent() { 823 url := "http://127.0.0.1:8999" 824 fmt.Println(g.Client().HeadContent(ctx, url, g.Map{ 825 "id": 10000, 826 "name": "john", 827 })) 828 829 // Output: 830 } 831 832 func ExampleClient_PatchContent() { 833 url := "http://127.0.0.1:8999" 834 fmt.Println(g.Client().PatchContent(ctx, url, g.Map{ 835 "id": 10000, 836 "name": "john", 837 })) 838 839 // Output: 840 // PATCH: form: 10000, john 841 } 842 843 func ExampleClient_ConnectContent() { 844 url := "http://127.0.0.1:8999" 845 fmt.Println(g.Client().ConnectContent(ctx, url, g.Map{ 846 "id": 10000, 847 "name": "john", 848 })) 849 850 // Output: 851 // CONNECT: form: 10000, john 852 } 853 854 func ExampleClient_OptionsContent() { 855 url := "http://127.0.0.1:8999" 856 fmt.Println(g.Client().OptionsContent(ctx, url, g.Map{ 857 "id": 10000, 858 "name": "john", 859 })) 860 861 // Output: 862 // OPTIONS: form: 10000, john 863 } 864 865 func ExampleClient_TraceContent() { 866 url := "http://127.0.0.1:8999" 867 fmt.Println(g.Client().TraceContent(ctx, url, g.Map{ 868 "id": 10000, 869 "name": "john", 870 })) 871 872 // Output: 873 // TRACE: form: 10000, john 874 } 875 876 func ExampleClient_RequestContent() { 877 url := "http://127.0.0.1:8999" 878 fmt.Println(g.Client().RequestContent(ctx, http.MethodGet, url, g.Map{ 879 "id": 10000, 880 "name": "john", 881 })) 882 883 // Output: 884 // GET: query: 10000, john 885 } 886 887 func ExampleClient_RawRequest() { 888 url := "http://127.0.0.1:8999" 889 response, _ := g.Client().Get(ctx, url, g.Map{ 890 "id": 10000, 891 "name": "john", 892 }) 893 fmt.Println(len(response.RawResponse()) > 100) 894 895 // Output: 896 // true 897 } 898 899 func ExampleClient_Delete() { 900 url := "http://127.0.0.1:8999" 901 r, _ := g.Client().Delete(ctx, url, g.Map{ 902 "id": 10000, 903 "name": "john", 904 }) 905 defer r.Close() 906 fmt.Println(r.ReadAllString()) 907 908 // Output: 909 // DELETE: form: 10000, john 910 } 911 912 func ExampleClient_Head() { 913 url := "http://127.0.0.1:8999" 914 r, _ := g.Client().Head(ctx, url, g.Map{ 915 "id": 10000, 916 "name": "john", 917 }) 918 defer r.Close() 919 fmt.Println(r.ReadAllString()) 920 921 // Output: 922 // 923 } 924 925 func ExampleClient_Patch() { 926 url := "http://127.0.0.1:8999" 927 r, _ := g.Client().Patch(ctx, url, g.Map{ 928 "id": 10000, 929 "name": "john", 930 }) 931 defer r.Close() 932 fmt.Println(r.ReadAllString()) 933 934 // Output: 935 // PATCH: form: 10000, john 936 } 937 938 func ExampleClient_Connect() { 939 url := "http://127.0.0.1:8999" 940 r, _ := g.Client().Connect(ctx, url, g.Map{ 941 "id": 10000, 942 "name": "john", 943 }) 944 defer r.Close() 945 fmt.Println(r.ReadAllString()) 946 947 // Output: 948 // CONNECT: form: 10000, john 949 } 950 951 func ExampleClient_Options() { 952 url := "http://127.0.0.1:8999" 953 r, _ := g.Client().Options(ctx, url, g.Map{ 954 "id": 10000, 955 "name": "john", 956 }) 957 defer r.Close() 958 fmt.Println(r.ReadAllString()) 959 960 // Output: 961 // OPTIONS: form: 10000, john 962 } 963 964 func ExampleClient_Trace() { 965 url := "http://127.0.0.1:8999" 966 r, _ := g.Client().Trace(ctx, url, g.Map{ 967 "id": 10000, 968 "name": "john", 969 }) 970 defer r.Close() 971 fmt.Println(r.ReadAllString()) 972 973 // Output: 974 // TRACE: form: 10000, john 975 } 976 977 func ExampleClient_PutVar() { 978 type User struct { 979 Id int 980 Name string 981 } 982 var ( 983 user *User 984 ctx = context.Background() 985 url = "http://127.0.0.1:8999/var/json" 986 ) 987 err := g.Client().PutVar(ctx, url).Scan(&user) 988 if err != nil { 989 panic(err) 990 } 991 fmt.Println(user) 992 993 // Output: 994 // &{1 john} 995 } 996 997 func ExampleClient_DeleteVar() { 998 type User struct { 999 Id int 1000 Name string 1001 } 1002 var ( 1003 users []User 1004 url = "http://127.0.0.1:8999/var/jsons" 1005 ) 1006 err := g.Client().DeleteVar(ctx, url).Scan(&users) 1007 if err != nil { 1008 panic(err) 1009 } 1010 fmt.Println(users) 1011 1012 // Output: 1013 // [{1 john} {2 smith}] 1014 } 1015 1016 func ExampleClient_HeadVar() { 1017 type User struct { 1018 Id int 1019 Name string 1020 } 1021 var ( 1022 users []User 1023 url = "http://127.0.0.1:8999/var/jsons" 1024 ) 1025 err := g.Client().HeadVar(ctx, url).Scan(&users) 1026 if err != nil { 1027 panic(err) 1028 } 1029 fmt.Println(users) 1030 1031 // Output: 1032 // [] 1033 } 1034 1035 func ExampleClient_PatchVar() { 1036 type User struct { 1037 Id int 1038 Name string 1039 } 1040 var ( 1041 users []User 1042 url = "http://127.0.0.1:8999/var/jsons" 1043 ) 1044 err := g.Client().PatchVar(ctx, url).Scan(&users) 1045 if err != nil { 1046 panic(err) 1047 } 1048 fmt.Println(users) 1049 1050 // Output: 1051 // [{1 john} {2 smith}] 1052 } 1053 1054 func ExampleClient_ConnectVar() { 1055 type User struct { 1056 Id int 1057 Name string 1058 } 1059 var ( 1060 users []User 1061 url = "http://127.0.0.1:8999/var/jsons" 1062 ) 1063 err := g.Client().ConnectVar(ctx, url).Scan(&users) 1064 if err != nil { 1065 panic(err) 1066 } 1067 fmt.Println(users) 1068 1069 // Output: 1070 // [{1 john} {2 smith}] 1071 } 1072 1073 func ExampleClient_OptionsVar() { 1074 type User struct { 1075 Id int 1076 Name string 1077 } 1078 var ( 1079 users []User 1080 url = "http://127.0.0.1:8999/var/jsons" 1081 ) 1082 err := g.Client().OptionsVar(ctx, url).Scan(&users) 1083 if err != nil { 1084 panic(err) 1085 } 1086 fmt.Println(users) 1087 1088 // Output: 1089 // [{1 john} {2 smith}] 1090 } 1091 1092 func ExampleClient_TraceVar() { 1093 type User struct { 1094 Id int 1095 Name string 1096 } 1097 var ( 1098 users []User 1099 url = "http://127.0.0.1:8999/var/jsons" 1100 ) 1101 err := g.Client().TraceVar(ctx, url).Scan(&users) 1102 if err != nil { 1103 panic(err) 1104 } 1105 fmt.Println(users) 1106 1107 // Output: 1108 // [{1 john} {2 smith}] 1109 }