Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] howto disable redirect? #171

Open
petke opened this issue Jun 3, 2016 · 6 comments
Open

[question] howto disable redirect? #171

petke opened this issue Jun 3, 2016 · 6 comments
Assignees

Comments

@petke
Copy link

petke commented Jun 3, 2016

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
Copy link
Author

petke commented Jun 13, 2016

[bump]

1 similar comment
@petke
Copy link
Author

petke commented Jun 28, 2016

[bump]

@ras0219-msft ras0219-msft self-assigned this Jun 28, 2016
@ras0219-msft
Copy link
Contributor

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

@zwaheed-rms
Copy link

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);

@deeringc
Copy link
Contributor

deeringc commented Sep 8, 2017

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

@drearyworlds
Copy link

drearyworlds commented Oct 9, 2018

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";
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants