2

I am trying to store multiple headers in 1 $HEADER shell variable and use it with CURL.

HEADERS="-H \"X-API-KEY: $API_KEY\" -H \"Content-Type: application/json\""
URL="https://myendpoint"

curl -v $HEADERS -X GET  $URL

I tried different combination like using "$HEADERS" instead of $HEADERS but it didn't work

Alex
  • 16,739
  • 1
  • 28
  • 51
Mubramaj
  • 641
  • 9
  • 14

1 Answers1

6

Try this, so that the parameters are recognized as separate entries:

HEADERS=(-H "X-API-KEY: $API_KEY" -H "Content-Type: application/json")
URL="https://myendpoint"

curl -v "${HEADERS[@]}" -X GET "$URL"
bill.lee
  • 2,207
  • 1
  • 20
  • 26
  • It worked perfectly! Thank you very much – Mubramaj Jul 23 '18 at 21:02
  • @wrlee, for future note -- in [How to Answer](https://stackoverflow.com/help/how-to-answer), see the bullet point regarding questions which *...have already been asked and answered many times before* under the heading "Answer Well-Asked Questions". – Charles Duffy Jul 23 '18 at 21:51
  • @CharlesDuffy It isn't always obvious to me when a question has already been been asked/answered. I assume they'd posed the question because they didn't find the answer, so I am willing to answer within the context in which they asked. – bill.lee Jul 23 '18 at 22:14
  • Understood. This one in particular is rather a FAQ, though -- you're likely to see it again (and again, and again...) if you hang around for long. (Indeed, it's a FAQ [in more places than here](http://mywiki.wooledge.org/BashFAQ/050)). – Charles Duffy Jul 23 '18 at 22:21