New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot unmarshal ISO-8859-1 encoded XML #481
Comments
I got it to work by updating the import (
+ "golang.org/x/net/html/charset"
)
func Unmarshalc(c *Client, ct string, b []byte, d interface{}) (err error) {
if IsJSONType(ct) {
err = c.JSONUnmarshal(b, d)
} else if IsXMLType(ct) {
+ decoder := xml.NewDecoder(bytes.NewReader(b))
+ if strings.Contains(string(b), "encoding=\"ISO-8859-1\"") {
+ decoder.CharsetReader = charset.NewReaderLabel
+ }
+ err = decoder.Decode(d)
- err = xml.Unmarshal(b, d)
}
return
} Is this something that could be added to the code ? |
@mjeco I think, it's better to expose XML marshal & unmarshal for the override similar to JSON. So that resty user can inject the custom function. |
@mjeco After client := resty.New()
client.XMLUnmarshal = func(data []byte, v interface{}) error {
decoder := xml.NewDecoder(bytes.NewReader(data))
decoder.CharsetReader = charset.NewReaderLabel
err = decoder.Decode(v)
} |
@jeevatkm Allowing to set a custom unmarshal function for XML is the best path forward. Thanks for this! |
Trying to use the automatic unmarshaling of XML with
SetResult()
is failing for me as the XML in the response hasencoding="ISO-8859-1"
set.Fails with the following error:
xml: encoding "ISO-8859-1" declared but Decoder.CharsetReader is nilexit status 1
Response example:
I am able to successfully unmarshal the above XML response with the below function.
Is it possible to set the
CharsetReader
to be used for the XML unmarshal?The text was updated successfully, but these errors were encountered: