Spring Framework
  1. Spring Framework
  2. SPR-5653

RestTemplate handles redirects incorrectly

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.0 M3
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      If I send a redirect (302 or 303) back the RestTemplate it will follow the redirect but the method of the redirected request is not GET. Instead, it is the method of the request that led to the redirect, in my case PUT and DELETE.

        Activity

        Hide
        Arjen Poutsma added a comment -

        This is using the default JDK SimpleClientHttpRequestFactory, probably? Not the Commons HTTP one?

        I actually think it would be better not to follow redirects at all. That way, every call on the WebServiceTemplate results in a single HTTP request, and not multiple. What do you think?

        Show
        Arjen Poutsma added a comment - This is using the default JDK SimpleClientHttpRequestFactory, probably? Not the Commons HTTP one? I actually think it would be better not to follow redirects at all. That way, every call on the WebServiceTemplate results in a single HTTP request, and not multiple. What do you think?
        Hide
        Arjen Poutsma added a comment -

        I just checked: the behavior for Commons HTTP Client seems to be to only follow redirects on GET requests, not for others. That makes sense, and I will change the SimpleClientHttpRequestFactory accordingly.

        Show
        Arjen Poutsma added a comment - I just checked: the behavior for Commons HTTP Client seems to be to only follow redirects on GET requests, not for others. That makes sense, and I will change the SimpleClientHttpRequestFactory accordingly.
        Hide
        Arjen Poutsma added a comment -

        Fixed.

        Show
        Arjen Poutsma added a comment - Fixed.
        Hide
        Aseem Deshpande added a comment -

        So Arjen, just to confirm:

        RestTemplate will follow 302 request blindly for a GET request right?I am catching the response thru ResponseExtractor and the status code I get is 200 and response body contains html content of the page being redirected to.

        IS there a way we can diable this behaviour. In httpClient we have

        httpMethod.setFollowRedirects(false);

        Something like this? I tried to add

        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod();
        method.setFollowRedirects( false );

        CommonsClientHttpRequestFactory cc = new CommonsClientHttpRequestFactory(client);
        RestTemplate rt = new RestTemplate(cc);

        When I catch response in responseExtractor, I still get status code 200 and content of the html page being redirected to. We really love restTemplate and don't want to use httpClient for this 1 weird GET request which issues 302 (which is not the standard)

        Show
        Aseem Deshpande added a comment - So Arjen, just to confirm: RestTemplate will follow 302 request blindly for a GET request right?I am catching the response thru ResponseExtractor and the status code I get is 200 and response body contains html content of the page being redirected to. IS there a way we can diable this behaviour. In httpClient we have httpMethod.setFollowRedirects(false); Something like this? I tried to add HttpClient client = new HttpClient(); GetMethod method = new GetMethod(); method.setFollowRedirects( false ); CommonsClientHttpRequestFactory cc = new CommonsClientHttpRequestFactory(client); RestTemplate rt = new RestTemplate(cc); When I catch response in responseExtractor, I still get status code 200 and content of the html page being redirected to. We really love restTemplate and don't want to use httpClient for this 1 weird GET request which issues 302 (which is not the standard)

          People

          • Assignee:
            Arjen Poutsma
            Reporter:
            Rob Harrop
            Last updater:
            Trevor Marshall
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 2 weeks, 6 days ago