Curl response

I am running multiple curl commands in a script for different functions. Once the first curl command response is below 400, then only next curl command should execute. Also at the same time the output of each curl should be appended to the output.txt file.

response=$(curl -i -H "content-type: application/json" -w "{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}') >> out.txt

This is giving a blank file and also not providing response code.

Whereas on using them separately it works
Below gives correct response code.

response=$(curl -i -H "content-type: application/json" -w "{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}')
echo  $response

Below gives correct output to a file.

curl -i -H "content-type: application/json" -w "{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}' >> out.txt

Any suggestions?

If curl receives a 400 error (or many other error states see man page) it exits with an exit code of 22

curl -i -H "content-type: application/json" -w "{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{''}' -o out.json
if [ $? -eq 22 ] ; then # see exit codes section of $( man curl )
curl "fallback_page"
fi

Thank you skrynesaver for your response. How can we just take the inputs to a variable and file through the above command. Reason I am saying this is as the for check is done in another part of the code and I don't want to touch it, just have to pass the res ponse code to it and also save the output of curl to a file( and keep on appending others to the same file)

you're missing a closing ) in your "command substitution".
Check your script at ShellCheck.net.

Yeah thank you for pointing it out. I just missed writing it here, I have that in the code though

response=$? would allow you to use the returned exit code later in the program.
Had a quick review of the man page there myself, it seems that you need to invoke curl with the -f flag in order for this error state to be propogated,

should probably be this:

response=$(curl -i -H "content-type: application/json" -w "{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}' >> out.txt)

Yes, Well this is now giving me output in the file but I am still not getting anything in response.

what are you expecting? The return code of curl?

curl -i -H ....
echo "my return code: [${?}]"

What will this give , http responses? I need http response number in a variable.

Sorry, the suggested will give you a command/curl return code - not "http response"
Looking at man curl:

       -w, --write-out <format>
              Make curl display information on stdout after a completed trans‐
              fer.  The  format  is a string that may contain plain text mixed
              with any number of variables. The format can be specified  as  a
              literal  "string",  or  you can have curl read the format from a
              file with "@filename" and to tell curl to read the  format  from
              stdin you write "@-".

              The  variables  present in the output format will be substituted
              by the value or text that curl thinks fit, as  described  below.
              All  variables are specified as %{variable_name} and to output a
              normal % you just write them as %%. You can output a newline  by
              using \n, a carriage return with \r and a tab space with \t.

I think you might be missing a %, i.e. -w '%{http_code}'.

@Casata just curious whether the above was of any value to you?

so the command becomes

response=$(curl -i -H "content-type: application/json" -w "{http_code}"\
-u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{''}' -o out.tmp -w %{http_code} 2>/dev/null)
cat out.tmp >> out.txt

@Skrynesaver, I don't follow the changes and the usage of the -w option.
Why exactly do we have TWO instances of -w with 2 different params:

1-w "{http_code}" - double-quoted with no %

2 -w %{http_code} - unquoted (exposed to shell glob-ing etc)

And how the returned http_code value will be different from the stdout redirected to out.tmp?

Wish the were a way to test this... Really curious...

Sorry, I'm all over the place there, missed the raw string format spec in the earlier command.