JSONError error in newman only on postman runner it works fine

Hi getting error in newman. Strange ( saw this error first time)

JSONError in test-script
I am getting error in newman script as


var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Item_NUMBER", jsonData.itemNumber);
postman.setEnvironmentVariable("guid", jsonData.guidId);
console.log(jsonData.itemNumber);
console.log(jsonData.guidId);

//var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Session_id", jsonData.customFields[208].stringValue);
console.log(jsonData.customFields[208].stringValue);

//Execute Tests
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
pm.test("Response time is 9 secs", function () {
    pm.expect(pm.response.responseTime).to.be.below(9000);
});

Hey @gpub1 as you can see in the second screenshot, the result you got is an HTML response, and hence when you try to JSON.parse() it, it fails with the error you observed.

Coming to the error - looks like the API endpoint expects a different Content type than the one being passed?
The HTTP code - 415 that was returned (as seen in the first screenshot) also indicates that an incorrect media type was sent to the server.

If this was working earlier, most likely there are changes to the API endpoint in use. If you have access to the API server folks, check with them OR check the latest API docs for the end-points you are using to make sure you are sending the request in the correct format.

Hi Amit, yes output does says 415. But in postman it works fine with single request and with runner too.

Prob is here with newman which is not understood.
image
Also the postman runner result
image

@gpub1 if things are working in the app but failing in newman, I would start by comparing the requests being sent - especially the headers to make sure both are sending the exact same request and exact same Content-Type

You can use the newman switch --verbose to output these details.
And in the app look at the Postman console ( View > Show Postman Console )

–verbose doesn’t give much info

Postman console is with no errors there for request or collection.

Its working fine on my local machine with newman run

Is there any prob with Jenkins not working there .

@gpub1 my bad, I should have provided more info on what you should be looking at.

when you run the collection using newman with verbose switch, you should see the headers being output

newman run --verbose COLLECTION-NAME.json

The output should show you the header’s being sent:

  ┌
  │ [
  │   { key: 'Content-Type', value: 'application/json', system: true },
  │   { key: 'User-Agent', value: 'PostmanRuntime/7.26.0', system: true },
  │   { key: 'Accept', value: '*/*', system: true },
  │   { key: 'Cache-Control', value: 'no-cache', system: true },
  │   { key: 'Postman-Token', value: '587a5db2-XXX-XXX-XXXX-XXXXXXXXXX', system: true },
  │   { key: 'Host', value: 'postman-echo.com', system: true },
  │   { key: 'Accept-Encoding', value: 'gzip, deflate, br', system: true },
  │   { key: 'Connection', value: 'keep-alive', system: true },
  │   { key: 'Content-Length', value: 92, system: true }
  │ ]
  └ 

In the Postman app, since the request is working fine - you won’t see any errors - but if you expand the request, you should see the headers being sent:

Things to compare between newman and Postman app

  • Check that similar headers are being sent and that the header values are similar
    – Especially the Content-Type header is what you should be looking at
    – You may temporarily want to comment out the test-script for newman run, so that the request completes and you can check the headers (if not being shown already)
  • Also check to make sure that any POST payload matches between newman and Postman app
    – for this you can use console.log to output the payload you are sending

I have a strong hunch it’s the Content-Type header in the request that may be causing the issue here.

Is it possible that Jenkins may have an older collection?
Or maybe it’s accessing the wrong API endpoint? (Say the hostname resolves to a different IP in Jenkins which may have an older API server deployed?)

Got it worked !! thanks !! Issue was with the Server , network related. that was the reason only 1st request works then 2nd wasn’t working since it was down (n/w) :slight_smile:

1 Like