Newman: SyntaxError: Unexpected token S in JSON at position 0

Created on 10 Oct 2018  路  1Comment  路  Source: postmanlabs/newman

Hi
I adapated this help to write postman responses to files (https://github.com/postmanlabs/postman-app-support/issues/3033#issuecomment-301758179). Here are files used
SIA_Ext_Gateway Additional Test Cases.postman_collection.json ,
writeToDiskSIA_EXt_GatewayAdditionalTestCasesKN.js

However when i run the files i get the following error

SyntaxError: Unexpected token S in JSON at position 0
at JSON.parse ()
at EventEmitter. (C:\npmnode_modules\writeToDiskSIA_EXt_GatewayAdditionalTestCasesKN.js:17:27)
at EventEmitter.emit (C:\npmnode_modules\newmannode_moduleseventemitter3index.js:203:33)
at Function.callbacks.(anonymous function) [as request] (C:\npmnode_modules\newman\lib\runindex.js:191:34)
at afterRequest (C:\npmnode_modules\newmannode_modulespostman-runtime\lib\runnerextensions\http-request.command.js:85:35)
at complete (C:\npmnode_modules\newmannode_modulespostman-runtime\lib\runnerextensions\http-request.command.js:137:33)
at C:\npmnode_modules\newmannode_modulespostman-runtime\lib\runnerextensions\http-request.command.js:182:29
at C:\npmnode_modules\newmannode_modulesasync\distasync.js:1140:9
at C:\npmnode_modules\newmannode_modulesasync\distasync.js:473:16
at replenish (C:\npmnode_modules\newmannode_modulesasync\distasync.js:1006:25)
at C:\npmnode_modules\newmannode_modulesasync\distasync.js:1016:9
at _asyncMap (C:\npmnode_modules\newmannode_modulesasync\distasync.js:1133:5)
at C:\npmnode_modules\newmannode_modulesasync\distasync.js:1219:16
at C:\npmnode_modules\newmannode_modulesasync\distasync.js:1046:16
at Object. (C:\npmnode_modules\newmannode_modulesasync\distasync.js:240:20)
at Object. (C:\npmnode_modules\newmannode_modulesasync\distasync.js:74:12)

One thing i can understand is that there is a problem in parsing the JSON response ? so can we say that it is not giving a proper JSON response ? or where the issue is
NewMan_response.txt

SIA_Ext_Gateway Additional Test Cases.postman_collection.json.txt
writeToDiskSIA_EXt_GatewayAdditionalTestCasesKN.js.txt
Thanks a lot

Most helpful comment

@kthulsidoss The error message indicates that one of the responses returned by your API was non JSON. In the file writeToDiskSIA_EXt_GatewayAdditionaTestCasesKN.js, there is a statement that pushes responses into an array:

results.push(JSON.parse(body));

This will fail if any of the response bodies is not JSON. There are two ways to rectify this: (you'll have to update the code in writeToDiskSIA_EXt_GatewayAdditionaTestCasesKN.js to fix the issue)

  1. If your API does not return JSON responses, the JSON.parse function call can be removed altogether, so that responses are pushed into the array directly, like so:
results.push(body);
  1. On the other hand, if your API does return JSON responses, but has intermittent non-JSON responses for fatal server errors, etc, you can include a try-catch sequence to safely handle malformed responses. This can be done as follows:
try {
  results.push(JSON.parse(body));
}
catch (err) {
   // the JSON.parse call failed, handle the error appropriately
}

>All comments

@kthulsidoss The error message indicates that one of the responses returned by your API was non JSON. In the file writeToDiskSIA_EXt_GatewayAdditionaTestCasesKN.js, there is a statement that pushes responses into an array:

results.push(JSON.parse(body));

This will fail if any of the response bodies is not JSON. There are two ways to rectify this: (you'll have to update the code in writeToDiskSIA_EXt_GatewayAdditionaTestCasesKN.js to fix the issue)

  1. If your API does not return JSON responses, the JSON.parse function call can be removed altogether, so that responses are pushed into the array directly, like so:
results.push(body);
  1. On the other hand, if your API does return JSON responses, but has intermittent non-JSON responses for fatal server errors, etc, you can include a try-catch sequence to safely handle malformed responses. This can be done as follows:
try {
  results.push(JSON.parse(body));
}
catch (err) {
   // the JSON.parse call failed, handle the error appropriately
}
Was this page helpful?
0 / 5 - 0 ratings