Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failure : retrofit.RetrofitError: 307 Temporary Redirect?

I am using the library com.squareup.retrofit:retrofit:1.9.0, in order to send data to my server from my android Application.

In fact,when I want to send a request to the server, I found this error:

failure : retrofit.RetrofitError: 307 Temporary Redirect

I try many idea but the same problem persist.

Please Expert help me to resolve this issue.

Regards

like image 744
lotfi Avatar asked Dec 16 '15 14:12

lotfi


2 Answers

Edit: if you must not use Retrofit 1.9, switching to a newer version (ie 2.0+) should handle the solution covered below automatically.


Looks like your HTTP client (on Android side) should handle this redirection by reading the Location value in the response header you are receiving when this happens, which supposed to contain a target redirection URL you ought to hit from the client again.

See their comment here.

So for now you need to implement this at the application level (which is hard with Retrofit 1, easier with the forthcoming Retrofit 2) or wait until OkHttp 3.1(ish) when we implement the newest spec.

See also what 307 means.

Fixing 307 errors - general The 307 response from the Web server should always include an alternative URL to which redirection should occur. If it does, a Web browser will immediately retry the alternative URL. So you never actually see a 307 error in a Web browser, unless perhaps you have a corrupt redirection chain e.g. URL A redirects to URL B which in turn redirects back to URL A. If your client is not a Web browser, it should behave in the same way as a Web browser i.e. immediately retry the alternative URL.

If the Web server does not return an alternative URL with the 307 response, then either the Web server sofware itself is defective or the Webmaster has not set up the URL redirection correctly.

See javadoc for Retrofit 1.9.0 to grab the Location header value(URL) from response;

http://static.javadoc.io/com.squareup.retrofit/retrofit/1.9.0/retrofit/client/Response.html#getHeaders--

// omitting null check for brevity
for (Header header : response.getHeaders())
{
    if (header.getName().equals("Location")) {
        redirectURL = header.getValue();
    }
}

// do a redirect to redirectURL
like image 51
Kerem Avatar answered Nov 03 '22 10:11

Kerem


I got the solution after a lot of efforts. May be this will help you. If you are getting this error "203 HTTP 307 Temporary Redirect" then this trick will help you. Append '/' to your web service at the end and then check this error goes away. I don't know the reason behind this but it works for me.

For me, my old request web service: https://mydomain/rest/Search. Using this URL I was getting "203 HTTP 307 Temporary Redirect" where I was able to get a response in POSTMAN and web browser.

My new request web service with the trick: https://mydomain/rest/Search/. This '/' resolve the issue.

like image 35
Ready Android Avatar answered Nov 03 '22 11:11

Ready Android