make-fetch-happen
make-fetch-happen copied to clipboard
SyntaxError: Unexpected token N in JSON at position 4 while parsing near '404 Not Found'
When making some tests with make-fetch-happen I faced the following problem:
SyntaxError: Unexpected token N in JSON at position 4 while parsing near '404 Not Found'
at JSON.parse (<anonymous>)
at parseJson (XXXX/index.js:7:17)
at consumeBody.call.then.buffer (XXXXXs/node-fetch-npm/src/body.js:96:50)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
TypeError
Trace: TypeError: Cannot read property 'contacts' of undefined
at getEmployees (XXXXXndex.js:157:32)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
at run (/UsersXXXXXX:25)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
I'm using a http proxy. The code look likes this:
try {
console.log('Now at employees page ' + x)
res = await fetch('https://www.XXXX.com/' + companyid + '/XXXX.json?filter=all&limit=50&offset=' + x, {
method: 'GET',
headers: headers,
proxy: 'https://XXXX'+sessionrand+':[email protected]:22225'
})
var source = await res.json()
} catch (err) {
if (err.name == 'FetchError' && res.statusText == 'Not Found') {
console.log("Profiledeleted: " + companyid)
return null
} else if (err.name == 'FetchError' && res.status == '429') {
console.log('Too many requests. Waiting and changing proxy.')
await Timeout.set(8000)
sessionrand = 'session-'+Math.floor(Math.random()*100000000)
continue
} else if (err.name == 'FetchError') {
console.error("FetchError: " + companyid)
console.log("Status Code: " + res.statusText)
continue
} else {
console.log(err)
}
I was using plain node-fetch before and just replaced it now with make-fetch-happen to add our proxy servers.
Do I miss something?
You need to do something like:
res = await fetch('https://www.XXXX.com/' + companyid + '/XXXX.json?filter=all&limit=50&offset=' + x, {
method: 'GET',
headers: headers,
proxy: 'https://XXXX'+sessionrand+':[email protected]:22225'
})
if (res.ok) {
var source = await res.json()
} else {
// Handle http errors here
}
^ That's the solution. It looks like your proxy setup is having issues, so you need to make sure the request is Ok before calling .json()
.