6

I would like to make a simple HTTP POST using Spring RestTemplate. the Wesb service accept JSON in parameter for example: {"name":"mame","email":"[email protected]"}

   public static void main(String[] args) {

    final String uri = "url";
    RestTemplate restTemplate = new RestTemplate();
    // Add the Jackson message converter
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    // create request body
    String input = "{   \"name\": \"name\",   \"email\": \"[email protected]\" }";
    JsonObject request = new JsonObject();
    request.addProperty("model", input);

    // set headers
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Authorization", "Basic " + "xxxxxxxxxxxx");
    HttpEntity<String> entity = new HttpEntity<String>(request.toString(), headers);

    // send request and parse result
    ResponseEntity<String> response = restTemplate
            .exchange(uri, HttpMethod.POST, entity, String.class);

    System.out.println(response);
}

When I test this code I got this error:

 Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 400 Bad Request

when I call webservice with Curl I have correct result:

 curl -X POST -H "Authorization: Basic xxxxxxxxxx" --header "Content-Type: application/json" --header "Accept: application/json" -d "{   \"name\": \"name\",   \"email\": \"[email protected]\" } " "url"
2
  • add the webservice code you are trying to reach and screenshot of postman or curl expression which is executed correctly... Commented Dec 2, 2015 at 15:23
  • I'haven't access to webservice. by using curl I can call ws using this command: curl -X POST -H "Authorization: Basic xxxxxxxxxx" --header "Content-Type: application/json" --header "Accept: application/json" -d "{ \"name\": \"name\", \"email\": \"[email protected]\" } " "url" Commented Dec 2, 2015 at 15:34

1 Answer 1

16

try to remove model from the code, as i can see in your curl request you didn't use model attribute and everything works. try this:

 public static void main(String[] args) {

    final String uri = "url";
    RestTemplate restTemplate = new RestTemplate();
    // Add the Jackson message converter
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    // create request body
    String input = "{\"name\":\"name\",\"email\":\"[email protected]\"}";


    // set headers
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Authorization", "Basic " + "xxxxxxxxxxxx");
    HttpEntity<String> entity = new HttpEntity<String>(input, headers);

    // send request and parse result
    ResponseEntity<String> response = restTemplate
            .exchange(uri, HttpMethod.POST, entity, String.class);

    System.out.println(response);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can the message converter convert a Java object into json string? Let's say input is an ojbect of type User. User = new User("name", "[email protected]");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.