Ask a Question

Data sink from rawResponse

tawakoli
Visitor

Data sink from rawResponse

I want sink Location data from following rawResponse to an excel file. can't figure it how.

here example of my rawResponse:

HTTP/1.1 201 Created
Date: Fri, 17 Feb 2023 21:40:43 GMT
Content-Length: 0
Connection: keep-alive
Server: Kestrel
Location: /PartTypes/d59eeffa-8a82-46c3-af7e-9a73197db648
api-supported-versions: 1.0

1 REPLY 1
ChrisAdams
Champion Level 2

Hi,

 

This should work.  There's probably a more elegant solution, but it should work.

 

Here's my test harness...

ChrisAdams_0-1677147127831.png

 

The first step, just simulates returning the raw response from a call.  I've used a multiline string to simulate...

def rawResponse = '''
HTTP/1.1 201 Created
Date: Fri, 17 Feb 2023 21:40:43 GMT
Content-Length: 0
Connection: keep-alive
Server: Kestrel
Location: /PartTypes/d59eeffa-8a82-46c3-af7e-9a73197db648
api-supported-versions: 1.0
''';

return rawResponse;

The next step is the interesting one.  It iterates over the multline string.  If the string contains "Location", then assign to a var.  The replace then removes the "Location:" prefix and finally returns the actual location value.

 

// Get raw response from previous step - this is where you'd pull it in from your service call...
def rawResponse = context.expand( '${Get Raw Response - Groovy Script#result}' )

// Create a var to hold location.
def location = "";

// Find the line containing Location and assign to location var.
rawResponse.eachLine {
if (it =~ /Location/) {
location = it;
}
}

// Remove the Location: prefix
location = location.replace("Location:", "");

// return Location to whatever has called this script. E.g. a data sink...
return location;

 

Finally, how to use in a datasink, just call the Groovy script!...

ChrisAdams_1-1677147344270.png

 

 

 

cancel
Showing results for 
Search instead for 
Did you mean: