Capture the response body values as variables

Hello!

For the purpose of executing a number of scenarios. In the POSTMAN service, you need to perform certain queries. After receiving a response from the end server. For each request, in the body of the response, its own unique ID will be formed. Which is necessary in the future:

  1. Fix. That is, in some way to remember for the purpose of further references to it.
  2. Use the unique ID received (in the response body) for subsequent requests.

Example request:

POST {{base_url}}prospectivesales/create/v4 

Response body:

{
    "ProspectiveSaleId": "018***e3-**6-4**4-9**4-476f*****b14",
    "PartnerCode": "b***0",
    "Additions": []
}

Since the data in the "ProspectiveSaleId":"018***e3-**6-4**4-9**4-476f*****b14", variable is unique.

It is this data - "ProspectiveSaleId" - that I need to retrieve. And use it in further queries.

Example follow-up request:

GET {{base_url}}prospectivesales/{{prospectiveSale_Id}}/canedit

Server response and corresponding error:

{"errors":[{"message":"The value '\"ProspectiveSaleId\"' is not valid."}]}

or

POST {{base_url}}prospectivesales/{{prospectiveSale_Id}}/postpone

Server response and corresponding error:
similar,

{"errors":[{"message":"The value '\"ProspectiveSaleId\"' is not valid."}]}

Please help me create the correct algorithm for subsequent requests using the body of the previous response to the request.

If you need clarification in my questions? Then I ask you to describe them to me.
Thank you for sure!

Hey @spacecraft-engineer6 :wave:

In your Tests tab, you can store that value in a variable:

pm.globals.set('prospectiveSale_Id', pm.response.json().prospectiveSaleId);

I’m just using the globals scope as an example but you could use a different variable scope if needed.

Once that’s stored, you can use that {{prospectiveSale_Id}} syntax in other requests.

Hey, Danny Dainton :wave:

I saved the line you specified:

pm.globals.set('prospectiveSale_Id', pm.response.json().prospectiveSaleId);

in the Test tab, as shown in the screenshot:

screenshot1

After that I checked the environment of the variable. And the values appeared there as indicated in the screenshot:

screenshots2

Did I do everything correctly, as you described?

I ask you because one of my subsequent requests received the following response, in the screenshot:

screenshot3

and in the console:

Response Body
{"errors":[{"message":"The value 'null' is not valid."}]}

What should I do next to resolve my issue positively?
Apparently I made a mistake somewhere?
Please tell me where exactly?

I made the mistake of thinking the response body key was in a certain format/casing.

It should be pm.response.json().ProspectiveSaleId and not .prospectiveSaleId. It’s creating the variable but it’s not picking up the value because the reference is wrong.

Danny has provided details on how to retrieve the element from your response.

However, in order to help yourself in the future.

Have you done through the Postman docs yet?

Using variables | Postman Learning Center

If you are new to Postman, then I would suggest a visit to the Learning Centre.

Overview | Postman Learning Center

Including the Postman Training links under “Other Resources”.

image

I would recommend the “Galaxy APIs 101” course first as it gets you used to the application features and the GUI.

Then the “Galaxy Testing and Automation” course which teaches you how to assert the responses. It includes defining and using variables from responses.

Targeting elements can be daunting at first. It will get easier the more you work with various responses. (As you’ve noticed, case sensitivity is a thing).

Tip when parsing responses.

Parse the response first.

const response = pm.response.json();

Then console log the elements under it, one level at a time until you eventually hit the element you are trying to target.

Once you have successfully targeted the element, you can then save to a variable.

1 Like

This was something I wrote on the forum a couple of years ago to try and help explain how to extract different pieces of data from a response.

The UI has slightly changed but it should all still work the same.

1 Like

Hey michaelderekjones :wave: ,
Thank you for the tips and pointers to my mistakes in creating the code and my queries. I have already visited and am going to continue to visit those educational texts and links that you described to me in your post. However, I have a specific task ahead of me. And unfortunately, I currently have very little time to delve deeply into the theory of writing code in JavaScript. And therefore, I study only the most necessary things for the purpose of solving my problem. The first point of which was correctly suggested to me by Danny Dainton.
However, I need to move on towards my goal.
Thank you again for your tips and links to training material. I will definitely use it the first time I need it!
All the best to you and success!

Hey Danny Dainton :wave: ,
Now I want to thank you for your hint, as a line of JavaScript code for the Test tab:

pm.globals.set('ProspectiveSale_Id', pm.response.json().ProspectiveSaleId);

I confirm that now the variable and the environment work correctly, for subsequent manipulations with the ProspectiveSaleId variable.
However, the variables described above and a successfully created POST request solve only one problem - the first one.
As I currently see in my algorithm, there will most likely be three such tasks. Although I can’t speak confidently about the number of tasks in the algorithm yet.
However, there are two more questions that I am going to definitely address.

Please write to me if you think that you need to create a separate topic to solve the following problems?

So, task 2:
It is necessary to remember (fix) the ProspectiveSaleId itself, extracted from the response body.

Task 3: To the remembered (fixed) in task 2 - Id. Further, you will need to contact him, if necessary. It is worth clarifying that there will be quite a lot of such IDs.
Up to 2000 per month. This is required by the conditions of my task. The validity period (lifetime) of such IDs is limited to 60 calendar days. After this period, these Ids become irrelevant and must be deleted. However, during the entire validity period (60 calendar days), each of the Ids may be needed at any time. And in this case, access to it will be necessary.

P.S.
I need to admit that I set this task and its implementation to myself. And therefore I am responsible to myself for its successful solution. There is no one to help me. That’s why I turned to you Danny. I hope that you understand me correctly. And also, as you probably already understood, I am, of course, new to the development project. And of course, what’s important to me is not the decision itself! Namely the code - JavaScript!! Who is behind this decision. Ideally, you would first learn JavaScript to a certain level. And only then, take on such complex tasks. However, this is exactly my way! And I won’t give up on him! Because only through the most difficult tasks and their solutions comes practice and understanding of the essence of building code.

With hope for your understanding!

What if you refuse to help me in the future?
Then I will understand you correctly.

Thank you again!

@spacecraft-engineer6

I really do recommend the Learning Centre resources I posted earlier, and in particular the “Galaxy Testing and Automation” resource.

It should explain how to retrieve data from a response, and then use it in subsequent requests.

Your follow on queries in your last post appear to be lost in translation. For example, the following block is not clear.

Task 3: To the remembered (fixed) in task 2 - Id. Further, you will need to contact him, if necessary. It is worth clarifying that there will be quite a lot of such IDs.
Up to 2000 per month. This is required by the conditions of my task. The validity period (lifetime) of such IDs is limited to 60 calendar days. After this period, these Ids become irrelevant and must be deleted. However, during the entire validity period (60 calendar days), each of the Ids may be needed at any time. And in this case, access to it will be necessary.

I recommend going through the Learning Centre resources first. If you are still struggling, then post what you have tried, and try to explain the logic of what you are trying to achieve. Break it down into the smallest steps possible. (The training will show you how to do this as the code examples are all like this - get used to doing this as it will help you with your logic).

Postman does use JavaScript under the hood. And if you are going to be writing (and understanding) scripts, then you will need a basic understanding.

WC3 Schools is a good place as any for JavaScript training. (It’s where I learnt the basics), but there are tons of online resources that are suitable for this task.

1 Like

Hey michaelderekjones :wave:

I looked through and studied the material on your recommendation “Galaxy Testing and Automation”.

I have completed all five steps, studied and taken into account.
I see and understand that there are many similarities between my tasks and the material you offer for study.
Review and take note of the information in the four steps. In the fifth step, where you need to familiarize yourself with the Test tab and study one hundred and nine lines of code. I’ve read all of this, several times.

However, you need to enter exactly what your link is. ?

I still don’t understand! And what does it mean to enter your environment parameter “{{your-collection-link}}”?

Therefore, I ask you to clarify this issue for me in more detail.

And also at one of the steps, it is described that it is possible to save the body of the response. And use it as a base for the next query. I figured this out just as poorly. Can I ask you to describe this issue to me in more detail?

P.S.
Sorry if my English doesn’t seem very good to you!
Of course, I would be happy to talk with you in real communication!! However, I believe that now this is practically impossible. And I’m very sorry that this is so!
However, I strive to obtain Knowledge in the direction I have indicated. And therefore, if you can continue to support and guide me in my training, then I will be very grateful to you! In addition, I understand that you are striving for a larger circle of specialists in the field of POSTMAN. If I understood you correctly.

I definitely thank you!