Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

SyntaxError: Unexpected token N in JSON at position 4 while parsing near '404 Not Found' #67

Open
JannikZed opened this issue Feb 21, 2019 · 2 comments

Comments

@JannikZed
Copy link

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?

@stuartf
Copy link

stuartf commented Jun 18, 2019

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
}

@zkat
Copy link
Owner

zkat commented Jun 18, 2019

^ 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().

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants