1

I am writing a shell script to post a JSON file to a URL.

The header is:

Content-Type: application/json
Accept: application/json
Account-Number: xxxxxxxx
Authorization: Basic eW91cl91c2VyX25hbWU6cGFzc3dvcmQ=

The above JSON string is saved into a .JSON file and my curl command should process it and get the desired response. I use the following command but it does not process successfully:

curl -H "Content-Type: application/json" -X POST -d "data=@$f" https://abcdef.com/test/example/v1/

$f has the JSON file name.

I have the following questions as well:

  1. Should my header and the JSON string be in the same JSON file? If the header is present in a differnt file, what should be its extension? and how do I process that using curl command?
  2. Is there any specific handling I should do for special characters like {,",: etc?

2 Answers 2

5

You're not sending all the headers. I don't believe there's an option to read the desired headers from a file.

Since you're scripting in bash, I'd store all the options in a shell array for readability:

curl_opts=(
    -H "Content-Type: application/json"
    -H "Accept: application/json"
    -H "Account-Number: xxxxxxxx"
    -H "Authorization: Basic eW91cl91c2VyX25hbWU6cGFzc3dvcmQ="
    -X POST 
    --data-binary "@$f"
)
curl "${curl_opts[@]}" https://abcdef.com/test/example/v1/
Sign up to request clarification or add additional context in comments.

10 Comments

FYI, this won't work. You can't post JSON like this with -d data=@file.
Thankyou for your response guys. I tried the --data -binary option and also put all the header contents, but still I get this error: "errors": "message": "The request failed authentication", "error_code": "API_001", "error_name": "Unauthenticated request" -- In JSON format. Can you please help? Here is my actual curl command: curl --data-binary "@$filename" -H "Content-Type:application/json" -H "Accept:application/json" -H "Account-Number:3283950684" -H "Authorization:Basic OTFiNDA0MjctYzJhNi00MGVmLTgzMzgtOWIzY2JjODE2N2E1OnhjNGJkZjNmMjVmMw==" -X POST digitalapi.auspost.com.au/test
So, the actual curl request is now succeeding. Now you have to properly encode the credential properly. auth=$(echo "username:password" | base64); header="Authorization: Basic $auth"
@satheeshv, please see my answer. You should use -u curl option instead of manually crafting your auth header.
Thanks glenn. The Authorization string I have given in the example is the base64 encoded string of the user:password. But still the authorization fails. So does it mean that the user:password could be wrong?
|
1

Assuming you want to post your JSON data exactly as-is (without further processing), you should use --data-binary. If preserving content like newlines is not necessary, you can use -d/--data, but in the form -d @file, like this:

filename=file.json
curl http://httpbin.org/post --data-binary "@$filename" --user "name:password" -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Account-Number: xxxxxxxx'

of course, after fixing all syntax errors you have in your JSON (like hanging commas, isolated single quotes, etc). Specifying data in form key=val (or key=@file), like you're trying, will not work (you can use it for form parameters with -F, though); see valid data part syntax in curl.

Note that when using --data-binary, default method is POST (so, no need to specify it with -X), but you'll have to specify the correct Content-Type.

Also, instead of manually calculating Authorization header for HTTP Basic Auth, just use the -u/--user <user:pass> option.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.