cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Endpoint from the Request

SOLVED
Highlighted
Contributor

Re: How to get Endpoint from the Request

When I started to fetch the value like this only but I tried this with the Response specific value, It started to throw me the error 

 

Script I use to get the response - GenerateToken#Response#$['data']['token']

 

The error I got is attached here. It seems like a syntax error which can work in the REST request parameter but not in the groovy script.

 

 

 

 

 

 

 

Highlighted
Super Contributor

Re: How to get Endpoint from the Request

Please attach the error you got.

Highlighted
Contributor

Re: How to get Endpoint from the Request

This is the error message I got :

Thu Jan 24 14:02:27 AEDT 2019:ERROR:org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script3.groovy: 29: unexpected token: data @ line 29, column 126.
'${GenerateToken#Response#$['data']['to
^
org.codehaus.groovy.syntax.SyntaxException: unexpected token: data @ line 29, column 126.

Super Contributor

Re: How to get Endpoint from the Request

What are you trying to achieve here?

 

${GenerateToken#Response#$['data']['to

 

I suppose, GenerateToken is the 'Request Name' and you are fetching token out of them. Could you please share the xml/JSON from which you are trying to fetch the token?

Highlighted
Contributor

Re: How to get Endpoint from the Request

Here is the response in JSON format

 

{
"data" : {
"token" : "sidayf8r4jhrdsuf97adif09rujkfhw.8regiu6sy9s7d6fsa87s87439ykhkhfjdsbfsdf"
}
}

Highlighted
Super Contributor

Re: How to get Endpoint from the Request

Ok. Then, you can try this code:-

 

//Conside API_Name is name of the testStep.
def response = context.expand('${API_Name#Response}')
def token = response.data.token
Highlighted
Contributor

Re: How to get Endpoint from the Request

I tried below code

 

//Conside API_Name is name of the testStep.
def response = context.expand('${API_Name#Response}')
def token = response.data.token

but it is also throwing an error message:-

 

Thu Jan 31 14:46:44 AEDT 2019:ERROR:groovy.lang.MissingPropertyException: No such property: data for class: java.lang.String

This error message is coming for the last line def token = response.data.token

 

Highlighted
Super Contributor

Re: How to get Endpoint from the Request

@ranujn 

 

You gave me this response right?

 

{
"data" : {
"token" : "sidayf8r4jhrdsuf97adif09rujkfhw.8regiu6sy9s7d6fsa87s87439ykhkhfjdsbfsdf"
}
}

And I provided the code based on that. If you are getting the error. In that case, "data" attribute is not there in your response. You can pass the reponse to JsonSlurper. please ad few more lines of code:-

 

 

 

import groovy.json.JsonSlurper

def slurper = new groovy.json.JsonSlurper()
def response = context.expand('${API_Name#Response}') def slurp = slurper.parseText(response)
def token = slurp.data.token //Considering "data" and "token" as JSON attributes.

 

 

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Script Wanted
The new challenge is here!

Create a script to сhange an XPath Match assertion to a JSONPath Match assertion for the TechCorner tag!


Scripts created in July:
- A script to generate an email when an assertion fails
- A script to change the request body before sending it to the server
Top Kudoed Authors