JSONError in Collection Runner on non-JSON response

I have a query that elicits a plain text response. In Postman I have configured the query’s headers to include Content-Type=text/plain, but when I run the query in Collection Runner I always get:
JSONError: Unexpected token ‘M’ at 1:1
MTR=20&PGTR=20P&authCode=240966&amount=1000&result=0&digest=ec0406d173c7301a632^

Collection Runner then stops instead of proceeding to run the next request in my collection.
When I force Collection Runner to run the next request, it fails to pick up a variable that was set by the previous query’s test script, even though I have “Keep variable values” checked. I am open to the possibility that these latter issues might be as a result of the first, although I have some doubt - the test script has clearly run because I get shown the results of its tests in Collection Runner, so the variable should have persisted.

The variable is set by this code:

var respData = JSON.parse(responseBody);
pm.variables.set(“PGTR”, respData.PGTR);

The following request accesses the variable using the standard {{PGTR}} syntax. I know it isn’t being picked up because the console shows that the {{PGTR}} (albeit URL-encoded) has been included in the query rather than substituted.

Anyone any ideas?

Hi @michael.brooks,

Welcome to the community! :wave:

I believe you mentioned the response is a non-JSON response, correct?

The error you are seeing is most likely due to you attempting to do JSON.parse() on a String ( assuming responseBody is “MTR=20&PGTR=20P&authCode=240966&amount=1000&result=0&digest=ec0406d173c7301a632^” right? )

More information on how JSON.parse() works can be found here:

If your responseBody is just the string “MTR=20&PGTR=20P&authCode=240966&amount=1000&result=0&digest=ec0406d173c7301a632^” , you could do something like var pgtr = responseBody.substring(12,14); // should return 20 here and then set the variable accordingly.

More information on substring can be found here: String.prototype.substring() - JavaScript | MDN.

Hopefully, this helps.

1 Like

Thanks Sabri - I’d like to say can’t believe I did something that stupid but I’d be lying if I did. It’s still sickening though. :face_vomiting:
I copied an example from the documentation into my test script without even thinking about it. It was of course written with the expectation that the response was formatted as JSON. D’oh!