Customize Marshal and Unmarshal

Default behaviour

Req uses the go standard library’s marshal and unmarshal function by default:

  • Use json.Marshal or xml.Marshal to marshal request body if you pass struct or map into Request.SetBody, Request.SetBodyJsonMarshal or Request.SetBodyXmlMarshal.
  • Use json.Unmarshal or xml.Unmarshal to unmarshal response body if you pass struct or map into Request.SetResult, Request.SetError or Response.Unmarshal.


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

json := jsoniter.ConfigCompatibleWithStandardLibrary

client := req.C().

// Similarly, XML functions can also be customized