Ilia Naleva
Ilia Naleva

Reputation: 39

How to add content of file, as part of a terminal command

I have the following command:

curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ],
  "from": 10,
  "size": 10
}
'

I'm trying to divide it into two parts, one part will be saved in a file. so i can run something like this:

curl -X GET "localhost:9200/bank/_search?pretty" file.txt

How can i achieve this?

Upvotes: 1

Views: 373

Answers (2)

Thomas
Thomas

Reputation: 182000

To send data that comes from a file, rather than a command line argument, use [email protected]:

curl -X GET "localhost:9200/bank/_search?pretty" [email protected]

From the curl(1) manual page:

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. Posting data from a file named 'foobar' would thus be done with -d, --data @foobar. When -d, --data is told to read from a file like that, carriage returns and new‐ lines will be stripped out.

For JSON, stripping line breaks like that fortunately doesn't matter.

Upvotes: 1

Thomas Sablik
Thomas Sablik

Reputation: 16447

You can read the file in place

curl -X GET "localhost:9200/bank/_search?pretty" $(cat file.txt)

Upvotes: 0

Related Questions