0

I am using Spring RestTemplate client to do a POST call to another application which handles this request as HTTpServletRequest. Problem is HTTpServletRequest is expecting a key value pair e.g.

String xmlString = request.getParameter("xml12")
//xmlString should be  "`<parent><child></child></parent>`" but coming as null.

Here is the code snippet of both ends -

My app -

String data = "`<parent><child></child></parent>`"

HttpHeaders header = new HttpHeaders()

header.setContentType(MediaType.APPLICATION_XML)

Map<String,String> bodyParamMap = new HashMap<String,String>();
bodyParamMap.put("xml123",data)

String reqBodyData = new ObjectMapper().writeValuesAsString(bodyParamMap)

HttpEntity<String> entity = new HttpEntity<String>(reqBodyData,header)

RestTemplate rt = new RestTemplate()

String response = rt.postForObject("url",entity,String.class)
//Getting response as 500

Other app -

HttpServletRequest request = new HttpServletRequest()
String xmlString = request.getParameter("xml123")
// xmlString is null 

I just want to know my mistake and how to pass my data string to post request so that request.getParameter("xml123") receives my data xml as String.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Please attach stacktrace - I can guess, that maybe : - String data - don't need additional single quotes - You could try 'postForEntity' - https://stackoverflow.com/questions/35461148/how-to-send-xml-post-requests-with-spring-resttemplate We absolutely need more information ;) Cheers ! – Fincio Nov 05 '20 at 08:38
  • String data doesn't have additional single quotes, while posting somehow got added. – TechRav Nov 05 '20 at 09:01
  • I don't have stacktrace other than log saying 'Internal Server error 500'. Other end I can't fetch the logs. – TechRav Nov 05 '20 at 09:02
  • The request in working fine with Postman when I do POST with url and adding xml123 and its value under 'x-www-form-urlencoder' section. Point to check is, it works only when I add it under mentioned section. Can somebody help me with this translation to RestTemplate code? – TechRav Nov 05 '20 at 12:21

0 Answers0