Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Yes, this is related to Getting curl to output HTTP status code? but unfortunately not the same.

In a script I would like to run:

curl -qSfsw %{http_code} URL

where the -f option ensures that the exit code is non-zero to signal an error. On success I want to get the (textual) output from the fetched file, whereas otherwise I want to use the error code.

Problem:

  • Due to race conditions I must not use more than a single HTTP request
  • I cannot use a temporary file for storage of the content

How can I still split the HTTP return code from the actual output?


Pseudo code:

fetch URL
if http-error then
  print http-error-code
else
  print http-body # <- but without the HTTP status code
endif
share|improve this question
    
Could you clarify why you want to request the HTTP status code at all when you're only interested in the exit code of curl or the response text. –  Daniel Beck Mar 20 '13 at 21:32
    
@Daniel Beck: who says I am not interested in either? I am just interested in the exit code to decide whether I want the HTTP error code or whether whether I want the content from the given URL as output. –  0xC0000022L Mar 20 '13 at 21:37
add comment

1 Answer

up vote 3 down vote accepted

There is no need to use a temporary file. The following bash script snippet send a single request and prints the exit code of curl and the HTTP status code, or the HTTP status code and response, as appropriate.

# get output, append HTTP status code in separate line, discard error message
OUT=$( curl -qSfsw '\n%{http_code}' http://superuser.com ) 2>/dev/null

# get exit code
RET=$?

if [[ $RET -ne 0 ]] ; then
    # if error exit code, print exit code
    echo "Error $RET"

    # print HTTP error
    echo "HTTP Error: $(echo "$OUT" | tail -n1 )"
else
    # otherwise print last line of output, i.e. HTTP status code
    echo "Success, HTTP status is:
    echo "$OUT" | tail -n1

    # and print all but the last line, i.e. the regular response
    echo "Response is:"
    echo "$OUT" | head -n-1
fi

head -n-1 (print all but the last line) requires GNU, doesn't work on BSD/OS X.

share|improve this answer
    
thanks for taking the time to answer. This isn't quite what I am looking for, but I think it gets me started in the right direction. I'm continuing to tinker with my script a bit more, taking your suggestions into account. –  0xC0000022L Mar 20 '13 at 21:41
    
@0xC0000022L Edited answer, maybe this works better? –  Daniel Beck Mar 20 '13 at 21:43
    
the last edit is what I came up with ;) ... yep GNU utils are fine for my case. +1 and accept. –  0xC0000022L Mar 20 '13 at 21:46
    
side-note - the exit code $? will not be set properly. My guess is that the assignment in my case local OUT=$(...) inside a function resets it to 0. –  0xC0000022L Mar 20 '13 at 22:06
    
@0xC0000022L If the OUT assignment is in a function, and you access $? after the function call returns, you lost that part -- $? now is the return code of the function. Make sure to return $? as last statement in the function, after the curl call/OUT assignment. –  Daniel Beck Mar 20 '13 at 22:09
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.