haf / Http.fs

A simple, functional HTTP client library for F#
322 stars 43 forks source link

Breaking Changes #144

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hello,

I have a project using 4.1.2 via nuget, it appears that from 4.1.2 to 5.0.0 Request.autoFollowRedirectsDisabled was removed, which is preventing me from upgrading. It appears that looking at the code, the entire concept of disabling redirect following has been removed.

The doco has not been updated to reflect this change.

It seems the change that removed it was here:

https://github.com/haf/Http.fs/blob/375ead66ae5da41359df2621d49838165a3b3493/HttpFs/HttpFs.fs#L372

ghost commented 6 years ago

For anyone wondering, here is the workaround:

    let httpClientWithNoRedirects () =
        let handler = new HttpClientHandler(UseCookies = false)
        handler.AllowAutoRedirect <- false
        let client = new HttpClient(handler)
        client.DefaultRequestHeaders.Clear()
        client

    module Request =
        let createWithNoRedirects = 
            let noRedirectClient = httpClientWithNoRedirects ()
            Request.createWithClient noRedirectClient

then Request.createWithNoRedirects Get url

The reason that this was probably removed from Http.Fs is probably that the underlying (newly added since Http.Fs 5.0.0) .NET HttpClient does not allow changing of this property after initialisation of the HttpClient object (an exception is thrown), however, creating NEW HttpClient objects is expensive and bad for performance.

Might require updating the front page documentation with this info.

ivpadim commented 6 years ago

@xenocons you will need to use the createWithClient method.

  let handler = new HttpClientHandler(UseCookies = false)
  handler.AllowAutoRedirect <- true
  let client = new HttpClient(handler)
  client.DefaultRequestHeaders.Clear()

Request.createWithClient client ...
ghost commented 6 years ago

As a drop in for existing code:

    let httpClientWithNoRedirects () =
        let handler = new HttpClientHandler(UseCookies = false)
        handler.AllowAutoRedirect <- false
        let client = new HttpClient(handler)
        client.DefaultRequestHeaders.Clear()
        client

    module Request =
        let autoFollowRedirectsDisabled h = 
            let noRedirectClient = httpClientWithNoRedirects ()
            { h with httpClient = noRedirectClient }

This should work, be aware you will be calling new HttpClient effectively twice per actual request

haf commented 6 years ago

@xenocons It would be great if you could paste this in the README via the edit-on-github functionality!

ghost commented 6 years ago

@haf, I have updated the readme. Perhaps we should consider implementing Request.createUrlWith to modify the HttpClientHandler before init as a parameter to allow for this:

Request.createUrlWith Post "https://www.a.com" (fun handler -> handler.AllowAutoRedirect <- false)

I am not sure how often this would be needed though, and it may be better to put the responsibility on the user to prevent API clutter.

haf commented 6 years ago

@xenocons Excellent, then just browse to the patch-1 branch of yours and click the little "create pull request" button, then send, so I can get it into the repo :)

We did it the way we did to avoid API-dependencies on types we may use to abstract over going forward. Sorry about the inconvenience.

ghost commented 6 years ago

No need for apologies, thanks for walking me thru it!