title: Why this externa web service call go into error only when the call is performed using Spring RestTemplate?
body: <p>I am working on a Spring project implementing a simple console application that have to call an external REST web service passing to it a parameter and obtaining a response from it.</p>
  
  <p>The call to this webservice is:</p>
  
  <pre><code>http://5.249.148.180:8280/GLIS_Registration/6
  </code></pre>
  
  <p>where <strong>6</strong> is the specified <strong>ID</strong>. If you open this address in the browser (or by <strong>cURL</strong> tool) you will obtain the expected error message:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;response&gt;
      &lt;sampleid&gt;IRGC 100000&lt;/sampleid&gt;
      &lt;genus&gt;Oryza&lt;/genus&gt;
      &lt;error&gt;PGRFA sampleid [IRGC 100000], genus [Oryza] already registered for this owner&lt;/error&gt;
  &lt;/response&gt;
  </code></pre>
  
  <p>This error message is the expected response for this request and I correctly obtain it also using <strong>cURL</strong> tool to perform the request.</p>
  
  <p>So I have to perform this GET request from my Spring application.</p>
  
  <p>To do it I create this <strong>getResponse()</strong> method into a <strong>RestClient</strong> class:</p>
  
  <pre><code>@Service
  @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
  public class RestClient {
  
      RestTemplate restTemplate;
      String uriResourceRegistrationApi;
  
      public RestClient() {
          super();
  
          restTemplate = new RestTemplate();
          uriResourceRegistrationApi = "http://5.249.148.180:8280/GLIS_Registration/7";
      }
  
      public ResponseEntity&lt;String&gt; getResponse() {
  
          ResponseEntity&lt;String&gt; response = restTemplate.getForEntity(uriResourceRegistrationApi, String.class);
          return response;
  
      }
  }
  </code></pre>
  
  <p>Then I call this method from this test method:</p>
  
  <pre><code>@Test
  public void singleResourceRestTest() {
      System.out.println("singleResourceRestTest() START");
  
      ResponseEntity&lt;String&gt; result = restClient.getResponse();
  
      System.out.println("singleResourceRestTest() END");
  }
  </code></pre>
  
  <p>But I am experiencing a very strange behavior, what it happens is:</p>
  
  <p>1)The call to my external web service seems that happens (I saw it from the web services log).</p>
  
  <p>2) The web service retrieve the parameter having value <strong>7</strong> but then it seems that can't use it as done without problem performing the request from the browser or by the shell statment: </p>
  
  <pre><code>curl -v http://5.249.148.180:8280/GLIS_Registration/7
  </code></pre>
  
  <p>But now, calling in this way, my webservice (I can't post the code because it is a WSO2 ESB flow) give me this error message:</p>
  
  <pre><code>&lt;200 OK,&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;response&gt;
      &lt;error&gt;Location information not correct&lt;/error&gt;
      &lt;error&gt;At least one between &amp;lt;genus&gt; and &amp;lt;cropname&gt; is required&lt;/error&gt;
      &lt;error&gt;Sample ID is required&lt;/error&gt;
      &lt;error&gt;Date is required&lt;/error&gt;
      &lt;error&gt;Creation method is required&lt;/error&gt;
  &lt;/response&gt;,{Vary=[Accept-Encoding], Content-Type=[text/html; charset=UTF-8], Date=[Fri, 05 May 2017 14:07:09 GMT], Transfer-Encoding=[chunked], Connection=[keep-alive]}&gt;
  </code></pre>
  
  <p>Looking the web service log it seems that performing the call using <strong>RestTemplate</strong> it have some problem to use the retrieved <strong>ID=7</strong> to perform a database query. </p>
  
  <p>I know it looks terribly strange and you can see: "The problem is of your web service and not of the Spring RestTemplate". This is only partially true because I implemented this custom method that perform a low level Http GET call, this <strong>callWsOldStyle()</strong> (putted into the previous <strong>RestClient</strong> class):</p>
  
  <pre><code>public void callWsOldStyle() {
      HttpURLConnection connection = null;
      BufferedReader reader = null;
  
      try {
          URL restAPIUrl = new URL("http://5.249.148.180:8280/GLIS_Registration/7");
          connection = (HttpURLConnection) restAPIUrl.openConnection();
          connection.setRequestMethod("GET");
  
          // Read the response
          reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          StringBuilder jsonData = new StringBuilder();
          String line;
  
          while ((line = reader.readLine()) != null) {
              jsonData.append(line);
          }
  
  
          System.out.println(jsonData.toString());
      }catch(Exception e) {
          e.printStackTrace();
      }
      finally {
      // Clean up
          IOUtils.closeQuietly(reader);
          if(connection != null)
              connection.disconnect();
      }
  }
  </code></pre>
  
  <p>Using this method instead the RestTemplate one it works fine and this line:</p>
  
  <pre><code>System.out.println(jsonData.toString());
  </code></pre>
  
  <p>print the expected result:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;response&gt;&lt;sampleid&gt;IRGC 100005&lt;/sampleid&gt;&lt;genus&gt;Oryza&lt;/genus&gt;&lt;error&gt;PGRFA sampleid [IRGC 100005], genus [Oryza] already registered for this owner&lt;/error&gt;&lt;/response&gt;
  </code></pre>
  
  <p>To summarize:</p>
  
  <ul>
  <li>Calling my WS from the browser it works.</li>
  <li>Calling my WS using cURL it works.</li>
  <li>Calling my WS using my <strong>callWsOldStyle()</strong> method it works.</li>
  <li>Calling my WS using the method that use <strong>RestTemplate</strong> it go into error when my WS receive and try to handle the request.</li>
  </ul>
  
  <p>So, what can be the cause of this issue? What am I missing? Maybe can depend by some wrong header or something like this?</p>