javascript - Converting ISO-8859-1 XML to UTF-8 -
i'm receiving xml data api. data converted json on server , saved in mongo database.
the problem is, xml receive encoded in iso-8859-1 , uses scandinavian letters å, ä , ö. encoding not specified in response headers.
i able view xml in browser, special letters show fine, in request response, special letters show question mark in black box (�) in console , in mongodb.
in request, send "content-type": "application/xml; charset=utf-8"
headers response still sent in iso-8859-1.
what options here? can somehow convert � right characters?
additional information
updated again:
response = http.call('get', 'http://peto-media.fi/tiedotteet/rss.xml', { headers: { "accept-charset": "utf-8" } }); data = buffer.from(response.content, 'binary').tostring('binary'); console.log(data);
sending content-type
in get
response doesn't make sense. header indicates media type of resource , server sends header in response get
request. also, sent in put
or post
requests in order indicate media type of sent content.
you should use accept-charset
header, tells server charset(s) client able understand:
accept-charset: utf-8
also, reason of xml
sent iso-8859-1
charset:
in versions of http/1.1, default charset (iso-8859-1) defined. no more case , each content type may have own default.
anyway, if api service you're using doesn't support accept-charset
can convert encodings yourself, before storing db.
added:
converting response content utf8
tricky thing: meteor's http.call
returns utf8
string, in response.content
have iso-8859-1
string treated utf8
string.
you have use buffer
convert back:
data = buffer.from(response.content, 'binary').tostring('binary');
i've tested myself url , works expected.
Comments
Post a Comment