Problem getting POST working with API

Hi - I’m having problems with my Postman Test Script…

// get the next available session number from the Gyst Server
var gystResponse = pm.response.json();
var jsonData = JSON.parse(gystResponse.varGystSession);
pm.collectionVariables.set('pmGystServerResponse', jsonData);
console.log(pm.collectionVariables.get('pmGystServerResponse'));

Here’s the key I generated to the Collection…

https://api.postman.com/collections/21044628-4b73b70a-9384-411c-a3c5-4e5bfaae90c4?access_key=PMAT-01HRHYG54836A2MDPVQR5N7MDD

I also uploaded a screen shot of the error and response from the server…

Any guidance much appreciated.

Your request is fine. It’s returning a 200 Ok status.

The error message is related to parsing JSON and is caused by your code in the tests tab. Which you do twice with pm.response.json() and also with JSON.parse.

Does varGystSession exist in your response?

Isn’t it just “gystSession”?

Therefore, it will be one of these lines causing the issues, so the best way to troubleshoot this is to console log these elements before you try and PARSE them.

Pretty sure you don’t need to parse the second time with JSON.parse, and on face value it looks like you have the element name wrong (but as you’ve only posted part of the response, I’m not 100% sure of this). You also need to target the “value” object first, so it would be pm.response.json().value.gystSession.

When you are trying to target elements, you should again use the console log. Target the top element and then add one element at a time to you get to your desired key/value. This way, its easier to spot when you’ve missed an element or spelt a key name wrong.

When posting code, including responses, please use the preformatted text option in the editor. It will stop everything getting aligned to the left, and it will also allow us to copy and paste to reproduce your response in a mock or using Postman Echo.

You’ve got your console log taking up most of the bottom half of the screen.

You can view the body of the response using the GUI like the following (which then allows you to cut and paste).

Using the performatted text option produces the following.

{
    "args": {},
    "data": {
        "@search.facets": {
            "State": [
                {
                    "count": 1,
                    "value": "Michigan"
                },
                {
                    "count": 1,
                    "value": "California"
                }
            ]
        },
        "value": []
    },
    "files": {},
    "form": {},
    "headers": {
1 Like

Many thanks for the detailed response @michaelderekjones ! It was not having the pm.response.json().value.gystSession set correctly that caused my problem. Once I added this, it worked fine. Thanks also for the insights on best practices here, very helpful guidance.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.