0

So I have this curl command. But I want to store the API-key in a file called api.txt and have it load where the value is in the curl command.

api.txt files contains

'X-Api:<API-KEY>'

This command works.

curl -H 'X-Api:<API-KEY>' -T file.zip "https://URL"

Tried:

curl -H “Content-Type: text/plain” -d “api.txt” -T file.zip "https://URL"

Got error:

  "errors" : [ {
    "status" : 401,
    "message" : "Unauthorized"
  } ]
}%      

Question:

  1. How can I load either the actual api key or the expected line from api.txt to parse it into the curl command?
  2. is this the way to do this? or a better way?
1
  • -d isn't going to work. -d sends POST request data. Your API seems to want the API Key in a HTTP request header (the -H option). HTTP request headers and POST data are two completely different things.
    – cas
    Commented Jul 13, 2022 at 4:40

2 Answers 2

1
curl -H "$(cat api.txt)" -T file.zip "https://URL"

Alternatively, to remove all percent symbols and single-quotes from input file:

curl -H "$(tr -d "'%" api.txt)" -T file.zip "https://URL"

or, to remove only the ' at the start and the '% at the end:

curl -H "$(sed -E "s/^'|'%$//g" api.txt)" -T file.zip "https://URL"

PS: I initially thought that this needed to be done in two lines, in order to avoid the trailing newline in api.txt:

API_KEY=$(cat api.txt)
curl -H "$API_KEY" -T file.zip "https://URL"

But bash seems to have improved its handling of newlines at the end of the input file in command substitution - i.e. it strips them if there's no further input. I'm using bash 5.1.16, you may have to use the two-line version if you're using a different shell or a different version of bash.

10
  • -H "$(cat api.txt)" seems an expensive way to do -H '@api.txt' where curl reads the file itself.
    – thrig
    Commented Jul 13, 2022 at 4:53
  • One cat process doesn't seem particularly expensive to me. Certainly less expensive than spending time learning about a minor convenience feature that only works in one particular program rather than a general-purpose shell feature like command substitution that works for pretty much everything.
    – cas
    Commented Jul 13, 2022 at 4:59
  • I still get the same error but I noticed when I "cat api.txt". There is a % at the end ex 'X-Api:<API-KEY>'% which might be the extra character that is cause the error. Is there a way to remove it?
    – Lacer
    Commented Jul 13, 2022 at 7:36
  • either edit the file to remove the % symbol, or use $(sed -e 's/%$//' api.txt) instead of $(cat api.txt).
    – cas
    Commented Jul 13, 2022 at 7:40
  • Ok used truncate -s -1 api.txt to remove that last character but % wasn't the last character it was actually '. So that means it's not working.
    – Lacer
    Commented Jul 13, 2022 at 7:45
1

curl supports @ to read the headers from a correctly formatted file; this will be more efficient than a forking out to cat and may allow the shell to be eliminated in favor of the simpler, more efficient, and less prone to security flaws exec(3) call to run curl instead of needing system(3) and a shell:

curl -H '@api.txt' -T file.zip "https://..."

The @ does not need quoting for a simple Bourne shell but is here to guard against some shell doing something with that character. If a fancy shell (e.g. zsh) is being used check the rules on quoting to see which characters have special meaning, or proactively quote everything.

1
  • this was the closest solution curl -H @api.key -T file.zip "https://"
    – Lacer
    Commented Jul 15, 2022 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .