Why am I getting SyntaxError: Unexpected token < in JSON at position 0
when trying to send a post
request that contains characters in body
like –
or ’
?
This is how I'm sending the request (I've added the hard coded body just as an example):
import request from 'request';
export default {
postScriptRequest(body) {
return new Promise((resolve, reject) => {
const options = {
'method': 'post',
'body': JSON.stringify({
"text": "Sending – instead of - and ’ instead of ' returns Unexpected token in JSON"
}),
'headers': {
'Content-Length': JSON.stringify(body).length
},
'url': 'my-url.com'
}
request(options, (error, response, body) => {
if(response.statusCode == 200) {
resolve(body);
}
else {
reject(response.statusCode);
}
});
});
}
}