2

I have the following scenario. I have an XML file:

query-users.xml

<?xml version="1.0"?>
<q:query xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3">
</q:query>

When executing the curl commend:

curl.exe --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST http://localhost:8080/midpoint/ws/rest/users/search -d @C:\Users\user\query-users.xml

I get the desired response in XML. I am trying to do the same POST request using RestTemplate from JAVA code:

try{    
    StringBuilder builder = new StringBuilder();
    builder.append("http://localhost:8080/midpoint/ws/rest/users/search");
    builder.append("?query=");
    builder.append(URLEncoder.encode("<?xml version=\"1.0\"?><q:query xmlns:q=\"http://prism.evolveum.com/xml/ns/public/query-3\"></q:query>"));

    URI uri = URI.create(builder.toString());

    restOperations.postForEntity(uri, new HttpEntity<String>(createHeaders("username", "pass")), String.class);
    logger.info(response);
    }catch(Exception e){
        logger.info(e.getMessage());
    }
}

I get Internal Servel Error . There is something that I am doing wrong passing the XML string to the POST request with RestTemplate, but I am not figuring out what it is.

Is there a way how to solve this?

Thanks

Blerta Dhimitri
  • 3,253
  • 5
  • 22
  • 33
  • Do you have access to the server's log? I see you are accessing localhost – TungstenX Feb 15 '17 at 10:11
  • 2
    You're not posting your xml in the request body, you're sending it as a query param... this post may help you http://stackoverflow.com/questions/35461148/how-to-send-xml-post-requests-with-spring-resttemplate – anders Feb 15 '17 at 10:26

1 Answers1

0

Your curl invocation and RestTemplate call are not equivalent. In first one you pass your xml as a a body of HTTP Request (this is what -d option does). In your RestTemplate you assign your xml to query and consequently HTTP Request has no payload and your data is encoded in URL.

If you want to pass your xml as a HTTP Body, you should use different HttpEntity constuctor: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpEntity.html#HttpEntity-T-org.springframework.util.MultiValueMap-

dchrzascik
  • 341
  • 1
  • 5