I'm using curl to request a URL that redirects to a different URL using a Location:
line like:
Location:/path/to/resource#name
As I understand it, that line in the redirect response is invalid per the HTTP specifications, so the overall curl call understandably fails (in this case, with a 400 response code). However, requesting the URL with wget or a web browser successfully renders the page (I assume through heuristics that fill in the absolute path or remove the anchor tag before the redirect).
Is there anything I can do to make curl do the same (do what is necessary to successfully follow the redirect, even if it is "officially" malformed)?
Edit: Some more details. The final response code is 400 (not 404 or something else). When I do a HEAD request (with curl -I -L
), I get a 302 Found
(with Location: /Error
) that redirects to a 500 Server Error
. However, if I do a regular request (without the -I
option but with the -L
option), I get a http_code
(in curl's --write-out
) of 400
. So it seems like HEAD requests in this case function differently than standard GETs.
Best Answer
As I understand it, curl doesn't follow Location headers by default.
You can enable such behaviour by using the
-L
or--location
switch. Like so:Note:
-I
is only used to show the headers rather than page content. I tested this using curl 7.21.6 running on Ubuntu.