JSONError in newman but not postman

Running requests successfully in postman, but get errors in newman.

newman version 3.9.3
macOS High Sierra 10.13.4

test code:
try {
responseJSON = JSON.parse(responseBody);
}
catch (err) {
console.log(err);
console.log(responseBody);
}

newman output excerpt:
POST https://api.xxx.com/payment/notify ⠆ ⠇ ┌
│ { type: ‘Error’, name: ‘JSONError’, message: ‘Unexpected token u in JSON at position 0’ }
│ null


response body in postman looks like this:
{“status_code”:1,“status_message”:“Success”}

@testmonger From the result of your code snippet, I can see that the response returned is null. Could you confirm the conditions under which your API returns such a response? Also, could you share the results of console.log(pm.response.json()); across the app and Newman?

@kunagpal Finally found a solution, related to authorization and environment variables. The environment variables used for authorization were working fine in postman, but required extra care when trying to access from newman.

(1) While running newman, I found it more reliable to specify the collection and environment as local json files rather than URLs. Example:

Use this in the newman shell script:
PMCOL=AUTO_XXX.postman_collection.json
PMENV=AUTO_XXX.postman_environment.json

Instead of this:
PMENV=“https://api.getpostman.com/environments/xxx-yyy-bbc0-4ef4-a4e3-cabffabad0c8?apikey=abc123
PMCOL=“https://api.getpostman.com/collections/xxx-yyy-bbc0-4ef4-a4e3-cabffabad0c8?apikey=abc123

(2) I need oauth credentials for some of the requests. But for the AWS elastic search requests (which are also included in the same collection), I had to include an additional parameter in the newman call: --insecure

So, the complete newman call that is successfully running for me looks like this:

newman run $PMCOL --folder $PMFOL -e $PMENV --ssl-client-cert xxx.crt --ssl-client-key xxx.key --ssl-client-passphrase xxx --insecure

1 Like