nhoover31
(Nick Hoover)
December 13, 2017, 10:35am
1
Hi,
I was wondering if there is a way to save part of a response as a variable.
I am testing a web service that posts an image, and returns a URL that contains the identifier for the image.
The only data in the response is the URL.
Is there a way to save part of that URL as a variable?
If not, the URL ends in “/image”, being able to drop that and saving the begining of the URL would be another option.
Thanks for the help.
dhoyt
(Dick Hoyt)
December 18, 2017, 5:25pm
3
Exactly what does your response body look like ?
I have an endpoint that returns a URL and I only test to see if the response starts with “https:”
Here is an example of the response body …
{
“trackUrl”: “https://blah.com ”
}
Here is my “test” …
function stringStartsWith (string, prefix) {
return string.slice(0, prefix.length) == prefix;
}
var resBody = JSON.parse(responseBody);
var testText = “”;
if(responseCode.code != 200) {
tests["TEST FAILED - Expected responseCode is 200 … responseCode received = "+responseCode.code] = responseCode.code === 200;
}
else {
if (stringStartsWith(resBody.trackUrl, “https:”)) {
testText = “TEST PASSED - URL starts with ‘https:’”;
}
else {
testText = “TEST FAILED - URL does NOT start with ‘https:’”;
}
tests[testText] = stringStartsWith(resBody.trackUrl, “https:”);
}
Hope this helps !
nhoover31
(Nick Hoover)
December 19, 2017, 8:53am
4
Hey, thanks for getting back to me!
The response body looks like this:
“https://AAA.BBB.com/CCC/DDD/ImageName/image ”
What I really want to do is just save the “ImageName” part.
If that’s not possible I could use the address up until the “/image” part.
nhoover31
(Nick Hoover)
December 19, 2017, 11:34am
5
I managed to get it working with
var jsonData = JSON.parse(responseBody);
var res = jsonData.replace("/image", “”)
var res2 = res.replace(“https://AAA.BBB.com/CCC/DDD/ ”, “”)
postman.setEnvironmentVariable(“publicImageURL”, res);
postman.setEnvironmentVariable(“publicImageUID”, res2);
dhoyt
(Dick Hoyt)
December 19, 2017, 8:17pm
6
That is great !!! Glad you found a way to solve your problem.