dimanche 15 mars 2015

How make http request using form data by rest template?

I can execute following http request using postman: enter image description here As you can see result:



{
"succes":false
}


Now I need to execute same request using restTemplate.


To achieve it I have wrote following code:



MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("secret", "6Le8YwMTAAAAADTIDNBvjxg-x83jt5QvPN-dFGWs");
map.add("response", recapchaResponse);

HttpHeaders headers = new HttpHeaders();
HttpEntity<?> entity = new HttpEntity<Map>(map, headers);
restTemplate.exchange("http://ift.tt/1AMIfbj", HttpMethod.POST, entity,ReCaptchaResponse.class);


When I executes following code in debug I see following response: enter image description here


As you can see response contains errors. Looks like response and secret is not received by server. why?


What do I wrong?


P.S.


I have wrote analog using httpClient



HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("http://ift.tt/1AMIfbj");
postMethod.setParameter("secret", "6Le8YwMTAAAAADTIDNBvjxg-x83jt5QvPN-dFGWs");
postMethod.setParameter("response", recapchaResponse);
client.executeMethod(postMethod);
String responseFromServer = new String(postMethod.getResponseBody());


It works good.


Aucun commentaire:

Enregistrer un commentaire