0

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\":\"akash@ab.com\"}]" 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
Akash Sharma
  • 330
  • 3
  • 18

1 Answers1

0

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] => akash@ab.com
        )

)
Luuk
  • 12,245
  • 5
  • 22
  • 33
  • Thanks Luuk. Tried using escape character but getting error. Please find the update section in my question. Please can you help. – Akash Sharma Apr 06 '22 at 08:30
  • I would expec it to work, see [1](https://stackoverflow.com/questions/11834238/curl-post-command-line-on-windows-restful-service) and [2](https://stackoverflow.com/questions/42797156/how-to-send-post-request-with-json-body-via-curl-from-windows-batch-script) – Luuk Apr 06 '22 at 08:36