1

I m using libcurl as a http client in my application. I can send and receive http messages with libcurl.

I want to extract the location field from the received http message when I got a message with http 302 as indicated above.

HTTP/1.1 302 Found
Location: http://192.168.201.1
Content-Length: 0

How I can extract the location field when I got http 302 message?

5
  • 1
    curl.haxx.se/libcurl/c/CURLINFO_REDIRECT_URL.html Commented Jan 4, 2016 at 10:54
  • @el.pescado Should I make redirect with libcurl to get the http url redirect info? because I do not want to. I just want to extract the info that's all. Commented Jan 4, 2016 at 10:58
  • 1
    Did you read the linked page? "receive the URL a redirect would take you to if you would enable CURLOPT_FOLLOWLOCATION". In other words it tells you where the redirect would go to if you were to follow it. In particular, if the redirect is a relative URL, it will take the original URL and figure out the absolute target URL. Commented Jan 4, 2016 at 11:34
  • @el.pescado it works. Thanks. Could you post your comment as answer and I will accept your answer (inchallah) Commented Jan 4, 2016 at 12:44
  • 2
    Based on this question, I've also added a getredirect example to the curl site showing how to use it: curl.haxx.se/libcurl/c/getredirect.html Commented Jan 4, 2016 at 14:14

1 Answer 1

3

If you need processed, absolute URL that you woult land on when following the redirect, use CURLINFO_REDIRECT_URL option. See also the getredirect example.

When you need raw value of Location header, you need to implement CURLOPT_HEADERFUNCTION callback and wait for the header to arrive.

Sign up to request clarification or add additional context in comments.

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.