camouflage
camouflage copied to clipboard
Question: How to get value from application/x-www-form-urlencoded body?
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 🙂
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, thanks for your suggestion. Unfortunately, the same result 🙁
That's strange. It worked for me!
Would you be open to sharing the complete contents of your .mock file?
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}}
Unfortunately, I don't seem to be able to replicate it.
- 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}}
- 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