0

I get an id from a POST request and I want to use that id in the GET and DEL requests. I don't want to do it manually; if there is automated way that my read and delete requests consume my POST request data, please let me know. Important note: I'm using Postman currently, so it would be great if someone can let me know how to do it in Postman.

1

This what the Postman variables do,

Assuming that your response data is:

{
   name: "something"
   user:{
      id:"12"
   }
}

in the test script section, add below command

let jsonData = JSON.parse(responseBody);
pm.environment.set("id", jsonData.user.id)   //this stores the id to env variable 'id'

now you can access the id value from the next requests like:

for non-sprit areas like body,header, url etc:

{{url}}/api/version/{{id}}

For test and pre-request scripts:

pm.environment.get("id")

enter image description here

Update Example:

I have set the variable "pdhide" from create user endpoint's test tab and now calling from pre-requisite tab for update endpoint:

enter image description here

| improve this answer | |
  • Thanks, this works great! Only thing though is; for the pre-request script i had to use pm.variables.get('id'). – Srinath Feb 5 at 3:42
  • Could you accept the answer by clicking the tick sign – PDHide Feb 5 at 7:12
  • It should work in pre-requisite also , i have updated the answer with the example that shows pm.enviroment.get works in pre-requisite. pm.variables.get also works , but its accessing the local variable and not the environment variables – PDHide Feb 5 at 7:39
0

Assuming that you need automate the below scenario

  1. You are making POST request. For example, you are creating a user
  2. Now yow want fetch the details of the user created using GET request
  3. And atlast Delete the user using Delete request

Now postman contains three tabs for user creation,user details and deleting user.

Follow the steps to automate the process

  1. Open tab "user creation" and navigate to test tab as shown in image below

enter image description here

  1. From snippet section which is right side of the application search for Code Snippet "Response Body:JSON value check" and click on it.Will update the below code snippet in Tests tab

    pm.test("Your test name", function () { var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100); });

  2. jsonData is the response from post request.Now parse the json to get required json data

  3. Now again from snippet section which is right side of the application search for Code Snippet "Set a global variable" and click on it.Will update the below code snippet in Tests tab

    pm.globals.set("your_variable_key", parsedjsondata);

  4. Now use this variable key in GET and DELETE request to parameritize

Also explore the below URL's for complete tutorials

https://learning.postman.com/docs/postman/scripts/test-scripts/

They have excellent documentation so spend time on reading so that you can master the Postman Automation Tool

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.