Huachao / vscode-restclient

REST Client Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=humao.rest-client
MIT License
5.3k stars 441 forks source link

Regex help #345

Open jyotibala5 opened 5 years ago

jyotibala5 commented 5 years ago

I need to extract code out of below response to use in next post request, however no regex is working ` Can anyone please help with this ? { "redirect_uri": "https://www.investec.com#code=X9zA4XSE&id_token=eyJraWQiOiJGMEQ4NTAxOUREM0NCRTEzMEY1QTg2QUQ1QTk5M0YzQjNDNzQxNTBBIiwidHlwIjoiSldUIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJ1cm46aW52ZXN0ZWM6b2I6YWNjOjg2ZTc3MjY2LWU3MTYtNDI5YS1iMzM4LTg1MTE5MTJmOGM2OSIsImF1ZCI6Inp3SUp5YUU3cE8xR2ZNQUNBN0FwYkxwUU9KZnQ2ZlZsIiwiYWNyIjoiIiwiY19oYXNoIjoiTm1FeE9XSTFZbUV3Tm1JNVpqRmlNemxsWkRRek5XVTBZVGczTVdWaFlUUT0iLCJvcGVuYmFua2luZ19pbnRlbnRfaWQiOiJ1cm46aW52ZXN0ZWM6b2I6YWNjOjg2ZTc3MjY2LWU3MTYtNDI5YS1iMzM4LTg1MTE5MTJmOGM2OSIsInNfaGFzaCI6Ik5XRTBPVGRsWkdSbFl6WXhOVFU1TXpkbE9EZzNaRFZqT0dWbFl6Z3laamc9IiwiaXNzIjoiaHR0cHM6Ly9hcGlzYW5kYm94LmludmVzdGVjLmNvbSIsImV4cCI6MTU1MzA3OTA0MSwiaWF0IjoxNTUzMDc1NDQxLCJub25jZSI6IjU3QjEyM0ZDLUVCQzctNDlGRS1BNzQ1LUQyODAzRUM5MURFOSIsImp0aSI6ImYxOTU1NDgzLWFjYTYtNDFlZi05ZmNkLTZhZmI3OWFkNzZjYyJ9.bdIwolRzNcUq2XWrFYf2iz-XZZ6A9LlfsZq1BTe2Sjl1G_L1CiF609C3XbxRGw1RGjjwUhgCs5jl-xVRV8PtaXHXcUXz5DPeKshDgPUfyBQBIIVyhWW9unGntWiLhNt6fonRbQ094cnHrqytR2FqT00lo6qz3ocEJ2GvBc_uFomFOWhXh7qSnW1F3oLh1VnskHQUtsWS1hJa3_Q-bP4pK5umesKsSPj6PELR7NrsEOG4ApGhj7EbIDf5ih5ToudegdoCh3u0iW3WESGn6vqV6_S7zUa-K78kL1CUUsQ8YRtWbxegVYByxIuEeu0DSverS9wTlLtsRn3X9fZv61jhDA&state=188592D7-B8E6-4774-AEAC-FDECA04E5B18" } something like @authorization_code = {{authorization_code.response.body.redirect_uri.???????}}

Huachao commented 5 years ago

@jyotibala5 the JSON path capability is supported by the jsonpath package, which doesn't support the regex capability

kmokrzycki commented 4 years ago

I have same issue with the plugin where I cannot extract parts of the response into variables. Is it at least possible to do the same in two steps ? e.g.

@Location= {{GetTjToken.response.headers.Location}}
@protocol = {{@Location.match(/^(https?)(.*?)/}}

Thanks

Huachao commented 4 years ago

@kmokrzycki actually I still prefer achieving this in only one step, that means JSONPath can support regex

jrussellsmyth commented 4 years ago

@Huachao Would be extremely useful to be able to use regex to extract parts of any response value, not just json - @kmokrzycki example is a plain header. I need exactly this for testing oauth flows, where we must extract the code value from a location header to submit a following request:

Location:http://host/path?code=NEED_THIS_VALUE_FOR_NEX_REQUEST

@value = {{response.header.location.match(/code=([^&^#^?]*)/)[1]}}

rjouhann commented 3 years ago

+1

petermallett commented 7 months ago

I figured out a way to make this work for my setup without needing to pull apart the location/redirect_uri. This might not work for all setups, but the below example is working for me.

where my location header from the response to the first request (clientrequest) looks like this: location: http://localhost:3000?session_state=<state value>&code=<code value>

@api_base_url = http://localhost:3000
@sso_url = https://yourSSOdomain.com/path
@sso_client = clientname
@sso_scope = scopename
@client_location = {{clientrequest.response.headers.location}}
@access_token = {{tokenrequest.response.body.$.access_token}}

###

# OAuth2 client_credentials
# @name clientrequest
POST {{sso_url}}/auth
Content-Type: application/x-www-form-urlencoded
Authorization: Basic {{$dotenv SSO_USER}}:{{$dotenv SSO_PASS}}

grant_type=client_credentials
&client_id={{sso_client}}
&scope={{sso_scope}}
&redirect_uri={{api_base_url}}
&response_type=code

###

# OAuth2 token
# @name tokenrequest
POST {{sso_url}}/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&client_id={{sso_client}}
&scope={{sso_scope}}
&redirect_uri={{api_base_url}}
&session_state={{client_location}}

It may be just a fluke or lucky that passing the non URL escaped location URL for session_state happens to work because both session_state and code values are in there, but I'm not sure.