cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

[question] howto disable redirect?

Open petke opened this issue 10 years ago • 6 comments

Hi

I GET a url that redirects (302) to a new url. cpprestsdk automatically redirects to the new url. In this case that's not what I want. I want to disable redirection, so that I can get at the http response header of the original url. (This header has important Set-Cookie information. I need to store this cookie in my application, so that I can use this cookie later for other GET calls)

How do I do this?

petke avatar Jun 03 '16 18:06 petke

[bump]

petke avatar Jun 13 '16 16:06 petke

[bump]

petke avatar Jun 28 '16 10:06 petke

You will need to use http_client_config::set_nativehandle_options() to set the WINHTTP_OPTION_REDIRECT_POLICY: https://msdn.microsoft.com/en-us/library/windows/desktop/aa384066(v=vs.85).aspx

ras0219-msft avatar Jul 26 '16 23:07 ras0219-msft

I was able to disable redirects on windows by using the code shown below, does anyone know how to achieve the same on Linux?

void CallbackFunc(native_handle handle) {
  // disable auto redirects
  DWORD dwOptionValue = WINHTTP_DISABLE_REDIRECTS;
  BOOL sts = WinHttpSetOption(handle, WINHTTP_OPTION_DISABLE_FEATURE, &dwOptionValue, sizeof(dwOptionValue));
  if (!sts) {
    std::cerr << "WINHTTP_DISABLE_REDIRECTS failed.\n";
  }
}

// later create http_client like shown below 
http_client_config config;
config.set_nativehandle_options(CallbackFunc);
http_client client(U("http://some_url/"), config);

zwaheed-rms avatar Nov 28 '16 21:11 zwaheed-rms

@zwaheed-rms, I don't believe that redirects are implemented in the asio-based http client which is used on Linux.

deeringc avatar Sep 08 '17 12:09 deeringc

Necro-posting just to show a more concise way to do it that worked for me:

http_client_config clientConfig;
clientConfig.set_nativehandle_options([&](web::http::client::native_handle nativeHandle) {
    // Disable auto redirects
    DWORD dwOptionValue = WINHTTP_DISABLE_REDIRECTS;
    BOOL sts = WinHttpSetOption(nativeHandle, WINHTTP_OPTION_DISABLE_FEATURE, &dwOptionValue, sizeof(dwOptionValue));
    if (!sts) {
        std::cerr << "WINHTTP_DISABLE_REDIRECTS failed.\n";
    }
});

drearyworlds avatar Oct 09 '18 19:10 drearyworlds