Customize Marshal and Unmarshal
On this page
Default behaviour
Req uses the go standard library’s marshal and unmarshal function by default:
- Use
json.Marshalorxml.Marshalto marshal request body if you pass struct or map intoRequest.SetBody,Request.SetBodyJsonMarshalorRequest.SetBodyXmlMarshal. - Use
json.Unmarshalorxml.Unmarshalto unmarshal response body if you pass struct or map intoRequest.SetResult,Request.SetErrororResponse.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)