'Post request with UTF-8 Encoding
I'm trying to POST a request to a url with XML data.
Some of the XML Data (aka the messageBody) has characters in Hebrew so I get this error -
TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
So it means I have to encode to UTF-8, how do I do that? Here's my code -
const messageBody = `סמס
נסיון
נסיון2`;
const xmlData =
`<?xml version="1.0" encoding="utf-8"?>
<Sms>
<User>
<Username>username</Username>
<ApiToken>key</ApiToken>
</User>
<Content Type="sms">
<Message>${messageBody}</Message>
</Content>
<Recipients>
<PhoneNumber>${cNumber}</PhoneNumber>
</Recipients>
<Settings>
<Sender>sender</Sender>
</Settings>
</Sms>`;
return request({
url: `someUrl?xml=${xmlData}`,
method: "POST",
headers: {
"content-type": "application/xml; charset=utf-8",
},
body: xmlData
}, function (error, response, body) {
if (error) {
return console.log(`Error Appeared : ${error}`)
} else {
return console.log(`Sent SMS : ${response}`);
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
