Reputation: 1789
I am using the Insomnia REST client to debug some mobile API. I need to extract part of one value in the response body and put it into a second request.
The request is an Oauth request for an authorization token. This request is normally sent from a mobile app.
The backend responds with a redirect_uri that contains the authorization_code. This format is used in oauth when the request (normally in PROD) comes from a mobile app.
{
"redirect_to": "redirect_uri=com.acme://oauth?client_id=abcde&response_type=code&scope=authenticate_user&authorization=H068bFHTXXXXXXXXXXX.....XXXXXXX"
}
How can I extract the value of the authorization parameter from this response in INSOMNIA?
** How can I extract only the part after "&authorization=....."**
I know how to extract the full value of the "redirect_to" JSON attribute. But then how to parse it? For example with a regex?
Upvotes: 0
Views: 3027
Reputation: 19
it is possible to do so, but it is kind of tricky. The most obvious way would be to use some plugin, but here is what I came up with:
Firt of all you need to make a variable which will save your response redirect_uri=com.acme ... =authenticate_user&authorization=H068bFHTXXXXXXXXXXX.....XXXXXXX
.
You can do this by pressing:
CTRL + E
This will open your environment variables, you can really make it in any enviroment, but probably the best would be to do in Base Enviroment
so it is accessable no matter which environment you are using.
Create a variable like this: placeholder variable
Then you need to get the redirect_uri
by pressing the following keys between the quotation marks:
CTRL+SPACE
This will bring up the quick menu, where you can type Response
and choose body attribute. Click on it, and set it like this.
I am pretty sure most people are familiar till this point.
Now you have saved the response to a variable you can use it. Navigate to the request where you want to use it, and you can type (basically in any field) {{ }}
.
Inside this variable write the following (with your own variable name).
{{ redirect_uri.match('authorization=([^&]+)')[1] }}
This is basically javascript
, using a simple regex
to match the url parameter.
If you did everything correctly you will see something like this, and by clicking on it you should see your token.
Note:
It might be red, until you get some values into the variable that you made.
I really hope it helps.
Upvotes: 1