简体   繁体   中英

Can't parse valid JSON - Unexpected token ( in JSON at position 0

I'm stuck in a problem that doesn't make ANY sense. I'm using the request module to do a request for other server that I own, and this server is clearly answering with the following JSON:

{"bucket":"name-of-my-bucket","prefix":"cb012af0-ac7a-414b-b474-a9c71cbec811/"}

If I log the type of the response, will be a string:

console.log(response.body) // string

But I can't parse to a javascript object, because it will throw the following error:

Unexpected token ( in JSON at position 0

I'm stuck on this for the last 3 hours, I already tried everything, including answering with a empty JSON, but the error persists. Do someone have any idea what it is?

const request = require('request-promise');

request.post('http://...', {
  formData: {
    // data here
  }
}).then(response => {
  // request.body is application/json
  console.log(response.body); // {"bucket":"name-of-my-bucket","prefix":"cb012af0-ac7a-414b-b474-a9c71cbec811/"}
  const data = JSON.parse(response.body);
}).catch(console.error);

Your json data is in "response" itself. so console.log(response) prints your json object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM