29

I am learning to use cURL and I need to run this in a console:

curl -XGET localhost:9200/library/book/_search?pretty=true -d {
    "query" : {
        "query_string" : { "query" : "title:crime" }
    }
}

But this is a multi-line command. How can I handle it to send a complete command?

Note that I understand I can put the content after -d in a file to run this command.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
curious1
  • 14,155
  • 37
  • 130
  • 231
  • What’s keeping you from simply _removing_ the line breaks (as they are only _formatting_, and should not matter at all for the actual content of that JSON data structure) …? – CBroe Jul 03 '14 at 14:51
  • _“I understand I can put the content after -d in a file to run this command”_ – quote cURL manual: _“When `--data` is told to read from a file like that, carriage returns and newlines will be stripped out”_ … so you would not have them in there in that case either. – CBroe Jul 03 '14 at 14:53
  • @CBroe, thanks so much for chiming in. Should I put the JSON structure in one line such as curl -XGET localhost:9200/library/book/_search?pretty=true -d {"query" : {"match_all" : {}} or curl -XGET localhost:9200/library/book/_search?pretty=true -d @{"query" : {"match_all" : {}} ? I got error in the first case, warning in the second. – curious1 Jul 03 '14 at 15:15
  • Does this answer your question? [Multiline curl command](https://stackoverflow.com/questions/32341091/multiline-curl-command) – Michael Freidgeim Aug 15 '20 at 10:02

3 Answers3

21

Install Git for windows. Use git bash to run the curl commands.

Harsha pps
  • 2,012
  • 2
  • 25
  • 35
  • This was easy - it worked immediately and proved that the url, apikey and json all worked and returned the right answer. Then I could focus on the Windows formatting differences. – Paul Evans Nov 12 '21 at 18:07
  • Worked like a charm. No need to escape characters; just copy and paste as-is. – nocdib Nov 14 '22 at 12:14
20

Sometime I have to use windows, try something like this :

curl -XPOST http://localhost:9200/_search -d^
"{^
    \"query\": {^
        \"query_string\": {^
            \"query\": \"year:2003\"^
        }^
    }^
}"

^ to extend a command to next line and
\" to escape " on the json

14

I didn't find any way so far to send command spanning multiple using curl on windows. However, I was able to find a way to get the task done. Below is how I would rewrite the command you are trying to achieve

curl -XGET localhost:9200/library/book/_search?pretty=true -d "{ \"query\" : { \"query_string\" : { \"query\" : \"title:crime\" }  } }"

Just make sure the /library/book/ is right index name.

Hoping to hear back if you are able to make it

Adnan
  • 276
  • 4
  • 11