Set Body

SetBody

Use SetBody to set request body, which accepts basic type such as string, []byte, int, uint, float and bool, and also support types such as io.Reader, req.GetContentFunc, it uses type assertion to determine the data type of body automatically.

// Create a client that dump request.
client := req.C().EnableDumpAllWithoutResponse()

// Send POST request with body.
client.R().SetBody(13.14).Post("https://httpbin.org/post")
:authority: httpbin.org
:method: POST
:path: /post
:scheme: https
content-type: text/plain; charset=utf-8
content-length: 4
accept-encoding: gzip
user-agent: req/v3 (https://github.com/imroc/req)

13.14

If it cannot determine, like map, struct and pointer, then it will wait and marshal to JSON or XML automatically according to the Content-Type header that have been set before or after, default to json if not set:

type User struct {
    Name  string `json:"name"`
    Email string `json:"email"`
}
user := &User{Name: "imroc", Email: "[email protected]"}
client.R().SetBody(user).Post("https://httpbin.org/post")
:authority: httpbin.org
:method: POST
:path: /post
:scheme: https
content-type: application/json; charset=utf-8
accept-encoding: gzip
user-agent: req/v2 (https://github.com/imroc/req)

{"name":"imroc","email":"[email protected]"}

SetBodyXXX

You can also use more specific methods such as SetBodyJsonString, SetBodyJsonBytes, SetBodyXmlString and SetBodyXmlBytes to avoid type assertions and improve performance, and also set the Content-Type automatically without any guesswork:

client.R().SetBodyJsonString(`{"username": "imroc"}`).Post("https://httpbin.org/post")
:authority: httpbin.org
:method: POST
:path: /post
:scheme: https
content-type: application/json; charset=utf-8
accept-encoding: gzip
user-agent: req/v2 (https://github.com/imroc/req)

{"username": "imroc"}

Similarly, Use SetBodyJsonMarshal or SetBodyXmlMarshal methods can marshal body and set Content-Type automatically:

cient.R().SetBodyXmlMarshal(user).Post("https://httpbin.org/post")
:authority: httpbin.org
:method: POST
:path: /post
:scheme: https
content-type: text/xml; charset=utf-8
accept-encoding: gzip
user-agent: req/v2 (https://github.com/imroc/req)

<User><Name>imroc</Name><Email>[email protected]</Email></User>

Customize Marshal Function

Req uses json.Marshal of go standard library to marshal request body if needed, you can customize with client.SetJsonMarshal or client.SetXmlMarshal:

// Example of registering json-iterator
import jsoniter "github.com/json-iterator/go"

json := jsoniter.ConfigCompatibleWithStandardLibrary

client := req.C()
client.SetJsonMarshal(json.Marshal).

// Similarly, XML functions can also be customized
client.SetXmlMarshal(xmlMarshalFunc)