Ask a Question

How do I extract the new resource created via a REST POST for use in a subsequent request?

SOLVED
AndyHughes
Regular Contributor

How do I extract the new resource created via a REST POST for use in a subsequent request?

The API I'm testing is RESTful so I can create a resource such as an account number by sending a POST to AndysAPI/newaccount

The code there generates an account number for me based on the details I've provided, but being RESTful does nothing but send back a success message in the raw response and the newly created URI ie AndysAPI/newaccount/1234.

That great but there's no message body. Therefore I can't update this resource by referencing it in a subsequent test using a GET DATA.

Any ideas how to get round this? I want to be able to extract 1234 from the raw response and use it in a subsequent request to update the account. At the moment I can only put the entire raw response in the next request because I can't specify the part I want.

11 REPLIES 11
msiadak
Community Hero

You can try to write a groovy script to parse the raw response to get the data required. That might work if it's not showing up in the non-raw response. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
AndyHughes
Regular Contributor

Im not that good with groovy but I could work out how to do it. But actually I think that what you're describing is already possible using 'Get Data' within the software. ie I can already grab the whole raw response. The problem is that I want a specific part of the raw response. Eg. currently Im getting:

HTTP/1.1 201 Created
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 0
Expires: -1 

HERE IS THE URI...at the end of which is the new resource I want to grab. 1234.

 

I just want to put 1234 into a parameter.

 

 

 

Since there's no data in the response and you have to go to the Raw response, Get Data may not be able to handle that accurately. Typically GetData, if you want to narrow down to one piece of information, needs to have a body to the response.

You could try using a Property Transfer step and see if that works. I ran through something similar where I had to parse a UUID out of my response headers, and groovy script ended up being the best way for me since I needed to store the UUID and use it more than once. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
AndyHughes
Regular Contributor

Yeh, I think this is the way to go because I cant see a way using the software to do it. 

But do you know the script I would need?

Thanks

I have a few snippets that you might be able to use for parsing out the data you want.  I'm going to have to fudge some of it since I do not know what your raw request looks like.

 

testStep = context.testCase.testSteps["Test Step Name"];

String request = new String(testStep.testRequest.response.rawResponseData);

// Set up a split-by-newline array for parsing line by line
String [] splitData = request.split("\n");

for (String eachSplit : splitData)
{
if (eachSplit.contains("AndysAPI/newaccount")
{
// Building this off of the example "AndysAPI/newaccount/1234"
// This creates an array with from the string, using "/" as the delimiter
def accountNumber = eachSplit.tokenize("/")[2];
log.info(accountNumber);
}
}

 

That should be enough to get you started.




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
AndyHughes
Regular Contributor

Thanks, I'll give it a whirl.

Awesome sauce. That worked. Thanks

All I had to do in addition was alter the [2] to a [5] to get the correct part of the URI and

 

return accountNumber;

 

to pass the result of the script back out.

If you wanted to store that account number, you can store it as a property in a properties test step too. Let me know if you need anything else. 🙂




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
AndyHughes
Regular Contributor

Yeh I did a property transfer with it.

Thanks

cancel
Showing results for 
Search instead for 
Did you mean: