PacificNW_Lover
PacificNW_Lover

Reputation: 5384

How to mimic curl REST call with Spring's RestTemplate?

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.

For example, for multiple inputs, this works:

curl -H "Content-Type: application/json" -X POST -d '{ "amount": 10000, "interestRate": ".28", "term": "12", "state": "Georgia"}' http://localhost:8080/webservices/REST/sample/loan

Returns:

Approved

For single inputs:

curl -H "Content-Type: application/json" -X POST -d "18" http://localhost:8080/webservices/REST/sample/age

Returns:

Approved

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.

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<String, String> input = new HashMap<>();
        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);
    }
}

All of these work except for the validCountry() test method:

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)

This is strange because this curl command works for the same call:

curl -H "Content-Type: application/json" -X POST -d 'US' http://localhost:8080/webservices/REST/sample/country

Returns:

Approved

Question(s):

  1. How can one mimic the rest call for the country (see above curl command) inside the validCountry() test method?

  2. Do I need to add or change a different value for HTTP Headers (inside setup() method)?

  3. Don't understand that validAge works by using the Integer wrapper class but the String doesn't?

  4. Is there a better way to do this using Spring's RestTemplate API?

Thank you for taking the time to read this...

Upvotes: 3

Views: 13339

Answers (1)

sag
sag

Reputation: 5461

You need to set the Content-Type to application/json. Content-Type has to be set in request. Below is the modified code to set the Content-Type

@Test
public void validCountry() {
    final String uri = "http://localhost:8080/webservices/REST/sample/country";
    String input = "US";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> request = new HttpEntity<String>(input, headers);
    String result = restTemplate.postForObject(uri, request, String.class);
    assertEquals("Approved", result);
}

Here, HttpEntity is constructed with your input i.e "US" and with headers.

I think this answers you questions 1,2 and 4. But not 3.

Upvotes: 1

Related Questions