如何在Shell脚本的CURL请求头中传入变量?API调用脚本头配置问题求助
Hey there! Let's tackle your two questions one by one to get you sorted out:
Using variables in curl headers is straightforward once you get the quoting right—here are the most common scenarios:
Basic variable usage
Define your variable first, then reference it inside double-quoted header strings (double quotes let the shell expand the variable value):# Define your variable AUTH_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" # Use it in curl curl -H "Authorization: Bearer $AUTH_TOKEN" https://api.example.com/endpointIf you use single quotes instead, the shell won't expand the variable—
'Authorization: Bearer $AUTH_TOKEN'would send the literal string$AUTH_TOKENinstead of its value.Variables with special characters
If your variable contains spaces, colons, or other special characters, wrap the entire header (including the variable) in double quotes to preserve the value correctly:CUSTOM_HEADER="User-Defined: Value with spaces & special chars!" curl -H "$CUSTOM_HEADER" https://api.example.com/endpointFor extra complex values (like multi-line strings), use
printfto construct the header safely:COMPLEX_VALUE="Line 1\nLine 2" FORMATTED_HEADER=$(printf "X-Multi-Line: %b" "$COMPLEX_VALUE") curl -H "$FORMATTED_HEADER" https://api.example.com/endpoint
Since you haven't shared your script or error output, here are universal steps to diagnose header problems:
Enable verbose mode to inspect actual headers
Add the-v(or--verbose) flag to your curl command. This will print every detail of the request, including the exact headers being sent. Look for lines starting with>—those are the headers your script is actually sending. Compare them to what you expect:curl -v -H "Authorization: Bearer $AUTH_TOKEN" https://api.example.com/endpointVerify variable values before sending
Add anechocommand right before the curl call to confirm your variables are set correctly. For example:echo "Debug: Authorization header is 'Authorization: Bearer $AUTH_TOKEN'" curl -H "Authorization: Bearer $AUTH_TOKEN" https://api.example.com/endpointThis will catch cases where variables are empty, misspelled, or have unexpected whitespace.
Check for quoting mistakes
This is the #1 culprit! If you wrap headers in single quotes, variables won't expand. If you forget quotes entirely, the shell might split your variable value into multiple arguments (especially if it has spaces). Always use double quotes around headers that include variables.Look for typos or whitespace errors
Small mistakes like missing colons (Authorization Bearer $TOKENinstead ofAuthorization: Bearer $TOKEN), extra spaces, or misspelled header names (e.g.,Authorizatioinstead ofAuthorization) can break header parsing. The verbose output will show these clearly.Test the command outside the script
Run the curl command directly in your terminal, replacing variables with their actual values. If it works manually but fails in the script, the issue is likely with how your script is setting or expanding variables (e.g., variable scope, unexported variables, or syntax errors in variable assignment).
内容的提问来源于stack exchange,提问作者Tiger




