Skip to content
This repository was archived by the owner on Apr 4, 2022. It is now read-only.

FollowRedirects option is ignored #92

Closed
jirihelmich opened this issue Feb 9, 2016 · 5 comments
Closed

FollowRedirects option is ignored #92

jirihelmich opened this issue Feb 9, 2016 · 5 comments

Comments

@jirihelmich
Copy link

I've just made a PR to demonstrate what's failing: #91

Code of request:

val response = Http("http://purl.org/linked-data/sdmx/2009/code#sex-M")
    .header("Accept", "text/turtle")
    .option(HttpOptions.followRedirects(true))
    .asString

Expected outcome: HTTP 200 with TTL
Actual outcome: HTTP 303

Response of the server (via cURL) with redirects disabled:

< HTTP/1.1 303 See Other
< Date: Tue, 09 Feb 2016 12:23:42 GMT
< Server: 1060 NetKernel v3.3 - Powered by Jetty
< Location: https://raw.githubusercontent.com/UKGovLD/publishing-statistical-data/master/specs/src/main/vocab/sdmx-code.ttl
< Content-Type: text/html; charset=iso-8859-1
< X-Purl: 2.0; http://localhost:8080
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Content-Length: 344
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
    <HEAD>
        <TITLE>303 Found</TITLE>
    </HEAD>
    <BODY>
    <H1>Found</H1>
         The resource requested is available <A HREF="https://raw.githubusercontent.com/UKGovLD/publishing-statistical-data/master/specs/src/main/vocab/sdmx-code.ttl">here</A>.<P>
    </BODY>
</HTML>

Response of the server (via cURL with -L option) is the expected outcome.

Btw., I forked your repo to write a test for this and start this issue with at least of a pull request of that failing test. However, I failed enormously as the test framework used by your library does not support headers in responses. Is that a wise choice?
That framework has issues section on GitHub disabled so I was not able to file an issue for that. Also, it's written in a way that the extension with headers capability is pretty hard outside of that library. To file a PR in a repo, wait for it being approved and file another PR to file an issue here is a bit complicated... So I gave up and went for filing this issue directly.

The test would be

@Test
  def redirectShouldFollowWithOption: Unit = {
    rProvider.expect(Method.GET, "/").respondWith(303, "text/text", "Found") // cannot add Location header to response here
      .expect(Method.GET, "/found").respondWith(200, "text/text", "OK")

    val response: HttpResponse[String] = Http(url).option(HttpOptions.followRedirects(true)).execute()
    assertEquals(200, response.code)
    assertEquals("OK", response.body)
  }

I will be happy to fix it if you point me in some direction. I will attach what I found so far in a minute.

@hoffrocket
Copy link
Member

That's a quirk of the underlying Java library. It won't follow redirects
from http to https.

I can add that support to the next release, but in the meantime you'll have
to handle the redirect yourself

http://stackoverflow.com/questions/1884230/urlconnection-doesnt-follow-redirect

On Tuesday, February 9, 2016, Jirka Helmich [email protected]
wrote:

I've just made a PR to demonstrate what's failing: #91
#91

Code of request:

val response = Http("http://purl.org/linked-data/sdmx/2009/code#sex-M")
.header("Accept", "text/turtle")
.option(HttpOptions.followRedirects(true))
.asString

Expected outcome: HTTP 200 with TTL
Actual outcome: HTTP 303

Response of the server (via cURL) with redirects disabled:

< HTTP/1.1 303 See Other
< Date: Tue, 09 Feb 2016 12:23:42 GMT
< Server: 1060 NetKernel v3.3 - Powered by Jetty
< Location: https://raw.githubusercontent.com/UKGovLD/publishing-statistical-data/master/specs/src/main/vocab/sdmx-code.ttl
< Content-Type: text/html; charset=iso-8859-1
< X-Purl: 2.0; http://localhost:8080
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Content-Length: 344
<

<TITLE>303 Found</TITLE>

Found

The resource requested is available here.

Response of the server (via cURL with -L option) is the expected outcome.

Btw., I forked your repo to write a test for this and start this issue
with at least of a pull request of that failing test. However, I failed
enormously as the test framework used by your library does not support
headers in responses. Is that a wise choice?
That framework has issues section on GitHub disabled so I was not able to
file an issue for that. Also, it's written in a way that the extension with
headers capability is pretty hard outside of that library. To file a PR in
a repo, wait for it being approved and file another PR to file an issue
here is a bit complicated... So I gave up and went for filing this issue
directly.

The test would be

@test
def redirectShouldFollowWithOption: Unit = {
rProvider.expect(Method.GET, "/").respondWith(303, "text/text", "Found") // cannot add Location header to response here
.expect(Method.GET, "/found").respondWith(200, "text/text", "OK")

val response: HttpResponse[String] = Http(url).option(HttpOptions.followRedirects(true)).execute()
assertEquals(200, response.code)
assertEquals("OK", response.body)

}


Reply to this email directly or view it on GitHub
#92.

@jirihelmich
Copy link
Author

Ah, OK. Thank you. I did not notice that, I was looking into options handling.

@hoffrocket
Copy link
Member

Hi, thanks for reporting this. I just pushed a 2.3.0 release with the fix.

@l15k4
Copy link

l15k4 commented Nov 7, 2017

I still get 301 Moved Permanently while GET requesting Django :

    Http("https://host/api/campaigns")
      .header("Authorization", s"Token $token")
      .option(HttpOptions.allowUnsafeSSL)
      .params(Map("format" -> "json", "is_active" -> "true", "limit" -> paginationLimit.toString))
      .asString

@l15k4
Copy link

l15k4 commented Nov 7, 2017

Got it, it is strange but it must end with slash Http("https://host/api/campaigns/")

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

No branches or pull requests

3 participants