You are including invalid HTTP headers in your request:
$header[ ] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[ ] = "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
On the first line, the list ends with a ,
– that is, an empty content type – which is the cause of 406 Not acceptable errors. The second line is not even a HTTP header.
If you were looking at Firefox HTTP conversations with a packet sniffer, you probably saw something like this:
Accept: text/xml,application/xml,application/xhtml+xml,
text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Since the second line starts with whitespace, they are treated as a single header by the server. They must also be passed as one header to curl:
$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
You can use http://echo.opera.com to compare the requests being sent.
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.
Best Answer
curl -I
will always return0
, if it managed to produce an output with the HEAD. You have two alternatives.The first is to use
curl -I --fail
instead, and check for exit code22
.If you're doing this in a Python script, it could look like:
The second is to actually ask just the HTTP status code, like this: