设置请求体

SetBody

使用 SetBody 来设置请求体,可以使用 string, []byte, int, uint, float, bool 等基础数据类型,也可以使用 io.Reader, req.GetContentFunc 这些类型的参数,会自动进行类型断言来获取数据内容:

// 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

如果参数不是上述类型,比如 mapstruct 以及指针,会等待并根据最后设置的 Content-Type 来自动将其 Marshal 成 JSON 或 XML 作为请求体内容,如果没设置,默认是 JSON:

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

你可以使用更具体方法设置请求体,比如 SetBodyJsonString, SetBodyJsonBytes, SetBodyXmlStringSetBodyXmlBytes,这样可以避免类型断言,提高性能,同时也会自动设置 Content-Type 请求头:

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"}

类似的, 使用 SetBodyJsonMarshalSetBodyXmlMarshal 可以将请求体显式的 Marshal 成 JSON 或 XML,同样也会自动 Content-Type 请求头:

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>

自定义 Marshal 实现

如果需要,Req 默认使用标准库的 json.Marshal 来将请求体 Marshal 成指定编码格式,你可以使用 client.SetJsonMarshal or client.SetXmlMarshal 来自定义 Marshal 的实现:

// 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)