I am trying to achieve same thing as this: How to use query parameter represented as JSON with Spring RestTemplate?, sending JSON string as a URL parameter in restTemplate.exchange().
The accepted answer mentions that sending JSON object as request parameter is generally not a good idea since you will probably face problem with curly brackets inside JSON. That is exactly what is happening when I am trying to make a GET call to an API. Since this is an API from another system, I cannot ask them to change the format and will have to call the GET endpoint, passing JSON as parameter. How can I achieve this in restTemplate.exchange() call?
Note: The mentioned related question does not guide on how to overcome this problem and I do not have enough reputation to comment on it to ask the author of the answer.
Answering my own question. While it is a bad idea to pass JSON like this in a query/url parameter, there is a workaround as suggested here: https://jira.spring.io/browse/SPR-9220?focusedCommentId=76760&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-76760.
Replicating the code here in case this link goes dead:
String url = "http://localhost:8983/solr/select?wt=json&indent=true&fl=*&q=*:*&fq={!geofilt}&sfield=venue_location&pt=28.0674,-80.5595&d=25";
URI uri = UriComponentsBuilder.fromUriString(url).build().encode().toUri();
System.out.println(uri);
// http://localhost:8983/solr/select?wt=json&indent=true&fl=*&q=*:*&fq=%7B!geofilt%7D&sfield=venue_location&pt=28.0674,-80.5595&d=25
Basically, instead of passing url having JSON query/url parameters as a string, pass it as a URI. Then call exchange method as before, but with URI instead of String:
restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String.class)
If this is 3rd party API and you cannot control or change JSON processing on backend side - there is no solution. Even if you will encode with URLEncoder - there is no guarantee that API backend would process such request correctly.
You can use URLEncoder class to encode the URL in exchange method, e.g.:
String url = "http://www.yoursite.com/api?param={\"some_key\":\"some_value\"}";
System.out.println(URLEncoder.encode(url, StandardCharsets.UTF_8.name()));
This will encode the characters (like braces and double quotes) and server then will decode it back to json.
Related
I am working on a Spring Boot application
I need to make a request to an external service, old and ill-conceived. The request take the form of a HTTP GET (or POST) call, but the payload, an xml content, need to be passed as a query parameter. For example,
GET http://ill-service.com/plain.cgi?XML_DATA=<request attribute="attributeValue"><content contentAttribute="plain"/></request>
Of course, the value of query param XML_DATA need to be URL encoded, and normally, the RestTemplate of Spring boot work good on that, following RFC 3986 (see http://www.ietf.org/rfc/rfc3986.txt).
Except that, as allowed by this RFC, '/' and '=' character are left in the param value, giving me the following query :
GET http://ill-service.com/plain.cgi?XML_DATA=%3Crequest%20attribute=%22attributeValue%22%3E%3Ccontent%20contentAttribute=%22plain%22/%3E%3C/request%3E
In a perfect wold, this would be good, but do you remember when I said that the service I am trying to call is ill-conceived ? In another world, it needs to have the full content of XML_DATA URL-encoded. In another words, it needs the following query:
GET http://ill-service.com/plain.cgi?XML_DATA=%3Crequest%20attribute%3D%22attributeValue%22%3E%3Ccontent%20contentAttribute%3D%22plain%22%2F%3E%3C%2Frequest%3E%0A
I am quite lost on how to instruct the rest template or the UriComponentBuilder I am using to do so. Any help would be greatly appreciated
Probably u can use spring's UriUtils class
Use java.net.URLEncoder to encode your XML payload first and then append the encoded payload.
Following the suggestion of Vasif, and some information about UriComponentBuilder I found the following solutions :
String xmlContent = "<request attribute="attributeValue"><content contentAttribute="plain"/></request>";
URI uri = UriComponentsBuilder.fromHttpUrl("http://ill-service.com/plain.cgi")
//This part set the query param as a full encoded value, not as query value encoded
.queryParam("XML_DATA", UriUtils.encode(xmlContent, "UTF-8"))
//The build(true) indicate to the builder that the Uri is already encoded
.build(true).toUri();
String responseStr = restTemplate.getForObject(uri ,String.class)
I am connecting to a third party tool that uses brackets inside their url parameters. I'm proxying someone else parameters, so I'm not building these parameters up and don't want to have to parse them exactly.
I've tried a basic encoding of parameters, this fails due to the third party application not knowing how to parse the encoding, it tries to read the encoded values directly as far as I can tell.
I realize this is not exactly how resttemplate is designed to work, but everywhere else in our code uses restTemplate and I don't want to bring in a new service simply for a basic proxy.
Is there any way to make resttemplate allow the brackets through without trying to do substitution on them?
You can use escape URL codes for special characters in the URL. e.g. If your URL is http://domainURL/{url-part}/rest-of-the-url then you could refer to it as http://domainURL/%7Burl-part%7D/rest-of-the-url.
Below is the code snippet for the reference:
LoginDetails loginDetails = restTemplate.getForObject(restServiceURL +
"%7Btest-paranthesis%7D/" + userUUID, LoginDetails.class);
For the above code the URL is http://localhost:8087/userLogin{test-paranthesis}/842063819010
I'm trying to use Uribuilder from:
javax.ws.rs.core.UriBuilder;
To update a URI. The issue is that the parameter name gets escaped when I use replaceQueryParam.
so:
UriBuilder uriBuilder = webResource.getUriBuilder().
replaceQueryParam("abcd!dcv, "wid").
replaceQueryParam("format", "json");
if there is already an existing "abcd!dcv" parameter in the Uribuilder, it will escape and add a new one. so it will become
?abcd!dcv=originalvalue&abcd%21cdv=wid
instead of
?abcd!dcv=wid
How should I get around this? Thanks!
URIBuilder is an abstract class and the implementation gets to decide which characters need special encoding and which do not. The URIBuilder we get from a WebResource is attempting to follow the guidelines of RFC 3986. On page 12, ! is listed as a sub-delimiter and this is why it is getting encoded. From my reading of the RFC, I don't think we should be using ! as part of a query parameter. For instance, Vaading uses ! to distinguish between sub-windows of the same application.
The simplest work around I can think of is to simply not use URIBuilder or use the fromURI method that takes a String as input. You can create the URI with everything except the part with the characters we don't want encoded, convert this to astring, manipulate astring to replace the query parameter and then call URIBuilder.fromURI(aString)
I have get muliple data from Json url using android.but i need to delete Particular data in json url using android.how to delete specific data from json url?.
I dont think that you can directly delete server data. Try to do some PHP code for that and you can invoke it from android.ie, you can separate functions by adding arguments in http request.
I don't know sure what you're trying to remove, if some data from the URL, or from the response. Anyway, if you know the exact string you're trying to remove, you could use the method replaceAll(String) from the String class.
For example:
String finalUrl = url.replaceAll("/partof/theurl/", "");
Hope this helps you.
I should use link. And it means that method GET and all parameters I can see in url. How I can hide parametrs from url like that
http://localhost:8080/MyApp/action.do?method=delete&id=0
And how after that I can get parametrs in java class.
You can't hide GET parameters from a URL. GET name value pairs have to be part of a request. However one thing you can certainly try is to encrypt your querystring into a string which only your code can decode on your host, for example
http://localhost:8080/MyApp/action.do?method=delete&id=0
could be
http://localhost:8080/MyApp/action.do?param=[ENCRYPTED/OBFUSCATED STRING]
then your application at the above url can decode that and convert back into name value pairs
That's the idea of GET method - to path parameters through URL, probably you should use POST.