title: How to mimic curl REST call with Spring's RestTemplate?
body: <p>Have an external Restful Web Service which takes in JSON payloads if its is more than one input but if its a single input it just needs the value.</p>
  
  <p>For example, for multiple inputs, this works:</p>
  
  <pre><code>curl -H "Content-Type: application/json" -X POST -d '{ "amount": 10000, "interestRate": ".28", "term": "12", "state": "Georgia"}' http://localhost:8080/webservices/REST/sample/loan
  </code></pre>
  
  <p>Returns:</p>
  
  <pre><code>Approved
  </code></pre>
  
  <p>For single inputs:</p>
  
  <pre><code>curl -H "Content-Type: application/json" -X POST -d "18" http://localhost:8080/webservices/REST/sample/age
  </code></pre>
  
  <p>Returns:</p>
  
  <pre><code>Approved
  </code></pre>
  
  <p>Using Spring Boot, tried to create a JUnit test, in order, to see if I can post to this external service using Spring's RestTemplate API.</p>
  
  <pre><code>public void RestWebServiceTest {
  
      private RestTemplate restTemplate;
      private HttpHeaders headers;
  
      @Before
      public void setup() {
          restTemplate = new RestTemplate();
          headers = new HttpHeaders();
          headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
      }
  
      @Test
      public void validLoan() {
          final String uri = "http://localhost:8080/webservices/REST/sample/Loan";
          Map&lt;String, String&gt; input = new HashMap&lt;&gt;();
          input.put("amount", "10000");
          input.put("interestRate", ".28");
          input.put("term", "12");
          input.put("state", "Georgia");
          String result = restTemplate.postForObject(uri, input, String.class);
          assertEquals("Approved", result);
      }
  
      @Test
      public void validAge() {
          final String uri = "http://localhost:8080/webservices/REST/sample/age";
          Integer input = 18;
          String result = restTemplate.postForObject(uri, input, String.class);
          assertEquals("Approved", result);
      }
  
      @Test
      public void validCountry() {
          final String uri = "http://localhost:8080/webservices/REST/sample/country
          String input = "US";
          String result = restTemplate.postForObject(uri, input, String.class);
          assertEquals("Approved", result);
      }
  }
  </code></pre>
  
  <p>All of these work except for the validCountry() test method:</p>
  
  <pre><code>org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
      at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
      at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
  </code></pre>
  
  <p>This is strange because this curl command works for the same call:</p>
  
  <pre><code>curl -H "Content-Type: application/json" -X POST -d 'US' http://localhost:8080/webservices/REST/sample/country
  </code></pre>
  
  <p>Returns: </p>
  
  <pre><code>Approved
  </code></pre>
  
  <p>Question(s):</p>
  
  <ol>
  <li><p>How can one mimic the rest call for the country (see above curl command) inside the validCountry() test method?</p></li>
  <li><p>Do I need to add or change a different value for HTTP Headers (inside setup() method)?</p></li>
  <li><p>Don't understand that validAge works by using the Integer wrapper class but the String doesn't?</p></li>
  <li><p>Is there a better way to do this using Spring's RestTemplate API?</p></li>
  </ol>
  
  <p>Thank you for taking the time to read this...</p>