'hitting a PATCH REST API endpoint using curl
I am trying to hit a REST API through curl on windows cmd and curl.exe on powershell
Below is how my curl command looks:
curl --insecure -g -v -o -X POST --header 'content-type:application/json' --header 'accept:application/json' --header 'Cookie:CTSESSION=<my ctsession>' --data
"[{"id":"90344","alertId":"xxxx","action":"CLOSE","assignee":"xxxx"}]" https://<web-sso url>
also tried using single quotes for request body as shown below
curl --insecure -g -v -o -X POST --header 'content-type:application/json' --header 'accept:application/json' --header 'Cookie:CTSESSION=<my ctsession>' --data '[{"id":"90344","alertId":"xxxx","action":"CLOSE","assignee":"xxxx"}]' https://<web-sso url>
but I get "status":400,"error":"Bad Request".
I think I am making some syntax error around the array [] in request payload. Have also tried using """ in request payload and also \ "" but nothing worked.
Please help me in figuring out my mistake
UPDATE:
Tried with below curl command
curl --insecure -g -v -o -X POST --header "content-type:application/json" --header "accept:application/json" --header "Cookie:CTSESSION=<my ctsession>" --data "[{\"id\":\"90344\",\"alertId\":\"afas\",\"action\":\"CLOSE\",\"assignee\":\"[email protected]\"}]" https://<websso url>
but getting following error
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('\' (code 92)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('\' (code 92)): was expecting double-quote to start field name
Solution 1:[1]
For parameter in Windows never use ', always use ". A " in a parameter needs to be escaped, so try:
curl --insecure -g -v -o -X POST --header "content-type:application/json" --header "accept:application/json" --header "Cookie:CTSESSION=<my ctsession>" --data "[{\"id\":\"90344\",\"alertId\":\"xxxx\",\"action\":\"CLOSE\",\"assignee\":\"xxxx\"}]" https://<web-sso url>`
I did try on my localhost, to post to POST.PHP:
<?php
print_r(json_decode(file_get_contents("php://input"), true));
I received following output (after removing the -o option):
Array
(
[0] => Array
(
[id] => 90344
[alertId] => afas
[action] => CLOSE
[assignee] => [email protected]
)
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
