camouflage icon indicating copy to clipboard operation
camouflage copied to clipboard

Question: How to get value from application/x-www-form-urlencoded body?

Open akoenig opened this issue 1 year ago • 5 comments

This is more a question on how to fetch one value from an application/x-www-form-urlencoded encoded request body. I thought that due to the fact that body-parser might be used, it would be possible to catch the value via

{{#is (capture from='body' using='jsonpath' selector='$.client_id) 'some-client-id'}}

but without luck. Then I tried to use the regex matcher via:

{{#is (capture from='body' using='regex' selector='client_id\=(.*)\&') 'some-client-id' }}

Unfortunately this was also not successful. Any pointer into the right direction is highly appreciated 🙂

akoenig avatar Sep 23 '22 18:09 akoenig

Have you tried {{#is request.body.client_id 'some-client-id'}}?

EDIT:

{{#is request.body.client_id 'some-client-id'}}
{
    "client_id": "{{capture from='body' using='jsonpath' selector='$.client_id'}}"
}
{{/is}}

shubhendumadhukar avatar Sep 25 '22 11:09 shubhendumadhukar

@shubhendumadhukar, thanks for your suggestion. Unfortunately, the same result 🙁

akoenig avatar Sep 26 '22 09:09 akoenig

That's strange. It worked for me!

Would you be open to sharing the complete contents of your .mock file?

shubhendumadhukar avatar Sep 26 '22 09:09 shubhendumadhukar

Sure, here it is 🙂

{{#is request.body.client_id 'valid-client-id'}}

HTTP/1.1 200 OK
Vary: Origin
cache-control: no-store
Content-Type: application/json; charset=utf-8
Content-Length: 1202
Date: Fri, 23 Sep 2022 17:09:08 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"access_token":"the-jwt","expires_in":3600,"token_type":"Bearer"}

{{else}}

HTTP/1.1 401 Unauthorized
Content-Type: application/json

{
    "response": "response if no valid client id is present."
}
{{/is}}

akoenig avatar Sep 26 '22 09:09 akoenig

Unfortunately, I don't seem to be able to replicate it.

  1. Created a test/POST.mock file with following content
{{#is request.body.client_id 'valid-client-id'}}

HTTP/1.1 200 OK
Vary: Origin
cache-control: no-store
Content-Type: application/json; charset=utf-8
Content-Length: 1202
Date: Fri, 23 Sep 2022 17:09:08 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"access_token":"the-jwt","expires_in":3600,"token_type":"Bearer"}

{{else}}

HTTP/1.1 401 Unauthorized
Content-Type: application/json

{
    "response": "response if no valid client id is present."
}
{{/is}}
  1. Both the curls result in expected responses
curl --request POST \
  --url http://localhost:8080/test \
  --header 'content-type: application/x-www-form-urlencoded' \
  --header 'user-agent: vscode-restclient' \
  --data client_id=valid-client-id
curl --request POST \
  --url http://localhost:8080/test \
  --header 'content-type: application/x-www-form-urlencoded' \
  --header 'user-agent: vscode-restclient' \
  --data client_id=invalid-client-id

shubhendumadhukar avatar Sep 26 '22 16:09 shubhendumadhukar