The most basic usage of an HTTP client is to send requests using several operation methods named after the HTTP Method. It’s important to note that the resulting object returned needs to execute Close to prevent memory overflow. Let’s look at some simple examples of HTTP client requests.

Sending a GET request and printing the return value

  1. if r, err := g.Client().Get(ctx, "https://goframe.org"); err != nil {
  2. panic(err)
  3. }
  4. defer r.Close()
  5. fmt.Println(r.ReadAllString())

Sending a GET request to download a remote file

  1. if r, err := g.Client().Get(ctx, "https://goframe.org/cover.png"); err != nil {
  2. panic(err)
  3. }
  4. defer r.Close()
  5. gfile.PutBytes("/Users/john/Temp/cover.png", r.ReadAll())

Downloading a file is very simple for small files. It’s worth noting that if the remote file is quite large, the server will return data in batches. Thus, the client will need to make multiple GET requests, each time requesting the batch file range length through the Header. Those interested can research the relevant details.

Sending a POST request and printing the return value

  1. if r, err := g.Client().Post(ctx, "http://127.0.0.1:8199/form", "name=john&age=18"); err != nil {
  2. panic(err)
  3. }
  4. defer r.Close()
  5. fmt.Println(r.ReadAllString())

When passing multiple arguments, users can use the & symbol to connect them. Note that parameter values often need to be encoded using gurl.Encode.

Sending a POST request with parameters as a map type and printing the return value

  1. if r, err := g.Client().Post(
  2. ctx,
  3. "http://127.0.0.1:8199/form",
  4. g.Map{
  5. "submit" : "1",
  6. "callback" : "http://127.0.0.1/callback?url=http://baidu.com",
  7. }
  8. )); err != nil {
  9. panic(err)
  10. }
  11. defer r.Close()
  12. fmt.Println(r.ReadAllString())

When passing multiple arguments, users can use the & symbol to connect them or directly use map (as previously mentioned, any data type is supported, including struct).

Sending a POST request with parameters as JSON data and printing the return value

  1. if r, err := g.Client().Post(
  2. ctx,
  3. "http://user.svc/v1/user/create",
  4. `{"passport":"john","password":"123456","password-confirm":"123456"}`,
  5. ); err != nil {
  6. panic(err)
  7. }
  8. defer r.Close()
  9. fmt.Println(r.ReadAllString())

As you can see, it’s very convenient to send JSON data requests via the ghttp client, simply by using the Post method to submit. When ContentType is not explicitly set, the client will automatically recognize the parameter type and set the request’s Content-Type to application/json.

Sending a DELETE request and printing the return value

  1. if r, err := g.Client().Delete(ctx, "http://user.svc/v1/user/delete/1", "10000"); err != nil {
  2. panic(err)
  3. }
  4. defer r.Close()
  5. fmt.Println(r.ReadAllString())

*Bytes and *Content Methods

Request methods ending with the Bytes and Content suffix are shortcut methods for directly obtaining the return content. These methods will automatically read the content returned by the server and automatically close the request connection. The *Bytes methods are used to obtain results of type []byte, while the *Content methods get results of type string. It’s important to note that if the request execution fails, the returned content will be empty.

Sending a GET request and printing the return value

  1. // The returned content is of type []byte
  2. content := g.Client().GetBytes(ctx, "https://goframe.org")
  1. // The returned content is of type string
  2. content := g.Client().GetContent(ctx, "https://goframe.org")

Sending a POST request and printing the return value

  1. // The returned content is of type []byte
  2. content := g.Client().PostBytes(
  3. ctx,
  4. "http://user.svc/v1/user/create",
  5. `{"passport":"john","password":"123456","password-confirm":"123456"}`,
  6. )
  1. // The returned content is of type string
  2. content := g.Client().PostContent(
  3. ctx,
  4. "http://user.svc/v1/user/create",
  5. `{"passport":"john","password":"123456","password-confirm":"123456"}`,
  6. )

*Var Method

Request methods ending with the Var suffix directly request and obtain HTTP API results as the generic type g.Var, making it easy to execute type conversions, especially converting request results into struct objects. It is generally used when the server returns data in JSON/XML format. By leveraging the returned g.Var generic object, you can perform automatic parsing based on your needs. Moreover, if the request fails or the request result is empty, an empty g.Var generic object will be returned, ensuring it doesn’t affect the conversion method invocation.

Example usage:

  1. type User struct {
  2. Id int
  3. Name string
  4. }
  1. // For a struct
  2. var user *User
  3. g.Client().GetVar(ctx, url).Scan(&user)
  1. // For an array of structs
  2. var users []*User
  3. g.Client().GetVar(ctx, url).Scan(&users)