1

I am trying to send a POST/PUT requests with CMD (command prompt in windows 10) using these commands:

1st:

curl -iX PUT -H "Content-Type: application/json" -d \"{\"name\": \"test number 1\", \"description\": \"a website test \", \"category\": \"test\", \"release_date\": \"2017-10-08T01:01:00.776594Z\"}\" localhost:8000/home/search/3

2nd:

curl -iX PUT -H "Content-Type: application/json" -d "{"name": "test number 1", "description": "A website test", "category": "test", "release_date": "2017-10-08T01:01:00.776594Z"}" localhost:8000/home/search/3

and using these commands I am getting a curl: could not resolve error, and when its done naming everything it couldn't resolve I get a server error status 500 I don't understand why its not working cause I tried these commands and they seemed to work before and they don't.

(I thought I had an error in my code and I tried the http request instead of curl and it worked fine with no problem getting the 200 OK status.).

enter image description here

Compo
  • 36,585
  • 5
  • 27
  • 39
Shad0w
  • 152
  • 2
  • 13
  • Shouldn't you have `http://` or `https://` before `localhost:8000/home/search/3`? –  Apr 26 '21 at 17:57
  • @JustinEzequiel No, not really. I thought of this as a solution and tried it as well and it still gave me the same error . – Shad0w Apr 26 '21 at 18:00
  • If you're on Windows, instead of figuring out how to properly escape quotes in Command Prompt, try `curl --data-binary @filename.ext ...` where `filename.ext` is a file with your JSON string. –  Apr 26 '21 at 18:32
  • The, e.g., "Could not resolve host: Plants vs Zombies" errors are all caused by the incorrect escaping of the double quotes after the `-d`. As I've been saying, use `--data-binary @filename.ext` and have your JSON in the file `filename.ext`. That way, you won't have to escape the double quotes! Why do you keep on ignoring my suggestion? –  Apr 27 '21 at 16:51
  • If you insist on NOT using a separate file for your JSON, then you'll need to escape the double quotes properly as in this [discussion](https://stackoverflow.com/questions/7172784/how-do-i-post-json-data-with-curl) with a special mention regarding single quotes not working on Windows. –  Apr 27 '21 at 16:59

2 Answers2

2

If NOT on Windows, the answer above by @Necklondon with the single quotes should work.

If on Windows, however, you'll need to escape the double quotes within the JSON string.

curl -iX PUT -H "Content-Type: application/json" -d "{""name"": ""test number 1"",""description"": ""A website test"",""category"": ""test"",""release_date"": ""2017-10-08T01:01:00.776594Z""}" http://localhost:8000/home/search/3

In addition, to illustrate how much simpler the CURL command becomes when using a separate file for your JSON...

C:\Users\...\Desktop>TYPE somefile.json
{
    "name": "test number 1",
    "description": "A website test",
    "category": "test",
    "release_date": "2017-10-08T01:01:00.776594Z"
}

C:\Users\...\Desktop>curl -iX PUT -H "Content-Type: application/json" --data-binary @somefile.json http://localhost:8000/home/search/3
  • Thanks its working I will be reading the discussion that you linked in the comments, and I also tried your methods on the comment and it worked, thanks as well. after that I just downloaded gitbash and tried my first command and @Necklondon command in and gitbash they worked. – Shad0w Apr 28 '21 at 10:25
1

Watch out for double quotes insides double quotes:

curl -iX PUT -H "Content-Type: application/json" -d '{"name": "test number 1", "description": "A website test", "category": "test", "release_date": "2017-10-08T01:01:00.776594Z"}' localhost:8000/home/search/3
Necklondon
  • 928
  • 4
  • 12
  • I did try this solution but it didn't work as well it gave me the same result which is the ````curl: could not resolve host````. – Shad0w Apr 26 '21 at 17:52
  • Strange because I get "$ curl -iX PUT -H "Content-Type: application/json" -d '{"name": "test number 1", "description": "A website test", "category": "test", "release_date": "2017-10-08T01:01:00.776594Z"}' localhost:8000/home/search/3 curl: (7) Failed to connect to localhost port 8000: Connection refused " which is normal – Necklondon Apr 26 '21 at 17:53
  • It is indeed strange this is why I was so confused when I got this error and what's more strange that it actually works with an http request but doesn't with a curl request which means I dont have an error in my code. – Shad0w Apr 26 '21 at 17:55
  • "curl: could not resolve host" is not the same as "error status 500" as per your question so what are you getting? –  Apr 26 '21 at 18:28
  • @JustinEzequiel I am getting both it first start naming every thing u see in the data ```` {"name": "test number 1", "description": "A website test", "category": "test", "release_date": "2017-10-08T01:01:00.776594Z"}" ```` and when its done it gives a 500 status . – Shad0w Apr 26 '21 at 19:22
  • I doubt that you're getting both. The server will only respond once. A "could not resolve host" error won't be able to respond with a 500 status code since it meant that CURL was unable to send the request as it was unable to determine the host to which to send the request. –  Apr 26 '21 at 19:24
  • 1
    Please do as I suggest and use `--data-binary @filename.ext` and use the full URL, e.g., `http://localhost:8000/home/search/3` and not just `localhost:8000/home/search/3`. –  Apr 26 '21 at 19:27
  • @JustinEzequiel I will. though it worked in http request but I didn't understand why it didn't in curl that's one reason why I asked this question. thanks and I will try to post a picture of the error in a moment. – Shad0w Apr 26 '21 at 19:31