0

I was curious as to whether it would be possible to have an alias that allows me to just call this entire cURL except for the PrimaryKey which is input or piped to the alias?

For instance, I have to paste into terminal the following command (JSON), with the only difference being the Primary Key varying (PRIMARYKEY123):

curl 'https://example.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://example.com' --data-binary '{"query":"{\n  getInfoById(Id: \"PRIMARYKEY123\") {\n    details {\n      images {\n        url\n      }\n    }\n  }\n}"}' --compressed

Would it be possible to have some sort of alias created where I can just enter the following into the command-line:

$ PRIMARYKEY123 | alias

Has anyone got any ideas on how I could achieve this or where I could start?

Thanks in advance for any help anyone can offer.

Thanks,

Stephen

Barmar
  • 741,623
  • 53
  • 500
  • 612
Stephen
  • 11
  • 3
  • The pipe executes the command `PRIMARYKEY123` and sends its output to the standard input of the `curl` command. That can't possibly be what you really want. – Barmar Oct 07 '19 at 18:11
  • There is some danger to parameterizing your payload using parameter expansion; if it were JSON, I would have suggested using `jq` in my answer, but it appears to be something else (GraphQL?). – chepner Oct 07 '19 at 18:13
  • @chepner thanks so much for your help - the output is piped into jq. GraphQL is correct :) – Stephen Oct 07 '19 at 18:25

1 Answers1

0

Define a function:

my_curl () {
  curl 'https://example.com' \
       -H 'Accept-Encoding: gzip, deflate, br' \
       -H 'Content-Type: application/json' \
       -H 'Accept: application/json' \
       -H 'Connection: keep-alive' -H 'DNT: 1' \
       -H 'Origin: https://example.com' \
       --data-binary '{"query":"{\n  getInfoById(Id: \"$1\") {\n    details {\n      images {\n        url\n      }\n    }\n  }\n}"}' \
       --compressed
}

Then pass the primary key as an argument:

my_curl PRIMARYKEY123
chepner
  • 497,756
  • 71
  • 530
  • 681