Customize Marshal and Unmarshal
On this page
Default behaviour
Req
uses the go standard library’s marshal and unmarshal function by default:
- Use
json.Marshal
orxml.Marshal
to marshal request body if you pass struct or map intoRequest.SetBody
,Request.SetBodyJsonMarshal
orRequest.SetBodyXmlMarshal
. - Use
json.Unmarshal
orxml.Unmarshal
to unmarshal response body if you pass struct or map intoRequest.SetResult
,Request.SetError
orResponse.Unmarshal
.
Customization
You can customize the marshal function with Client.SetJsonMarshal
or Client.SetXmlMarshal
if you need more advanced customization:
// Example of registering json-iterator
import jsoniter "github.com/json-iterator/go"
json := jsoniter.ConfigCompatibleWithStandardLibrary
client := req.C().
SetJsonMarshal(json.Marshal).
SetJsonUnmarshal(json.Unmarshal)
// Similarly, XML functions can also be customized
client.SetXmlMarshal(xmlMarshalFunc).SetXmlUnmarshal(xmlUnmarshalFunc)