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