Parsing and encoding URL before sending to Rest webservice - java

I am developing an App where I am sending get request to REST web service using
#RequestMapping(value = "myURLpattern", method = RequestMethod.GET, produces = "image/png")
I came across a bug if somebody enters # in the URL query String it breaks from there
http://myserver.com:8080/csg?x=1&y=2#4&z=32&p=1
So I tried to encode it via filter HTTPRequestWrapper so that # will be replaced by %23 using URLEncoder.
My problem is for encoding URL, first I need to read the URL(request.getRequestURL()).
and getURL again can't read string after #.
request.getRequestURL() will return only (http://myserver.com:8080/csg?x=1&y=2)
Is there any other way to parse the complete url and encode it before sending to REST web service?

That is not a bug. Check this:
What is the meaning of # in URL and how can i use that?
A # in the url query string is wrong. You should encode it on client side, before sending it to your server. See this one: How to pass '#' in query string
It is in asp, but you should find the java equivalent.
Something to get you started can be this one Java URL encoding of query string parameters

Related

Receiving percentage encoded url

I am invoking a GET method on a percentage encoded URI , but my rest controller is not able to handle it. It throws Internal Server Error. How am i suppose to handle encoded uri on rest controller side using #RequestParam
Use String in #RequestBody, and use URLDecoder to decode in UTF-8.
Like this - URLDecoder.decode(value, StandardCharsets.UTF_8.toString()).
You can use base64 as well. Check this answer.

How to use + and ( or ) with url s query parameter using IntelliJ

I’m having a use case where I need to get response from server where the url looks like the below
https://urlname/path?query=test.project+IN+(integer value)+AND+test.folder+IN+(integer value).
This url returns json as response body n this is working with postman and browser.
But I’m getting status code 500 when I hit this from IntelliJ using io.restassured.response.Response class methods as below
Response res = given()
.auth()
.contentType(“json”)
.queryParam(query,param)
.get(url);
Try this code. This will access the url https://urlname/path?query=test.project+IN+(1234)+AND+test.folderId+IN+(5433)
String query = "query";
String params = "test.project+IN+(1234)+AND+test.folderId+IN+(5433)";
Response res = given().contentType(ContentType.JSON).auth().basic("", "").queryParam(query, params)
.urlEncodingEnabled(false).get("https://urlname/path");
Found the solution or rather I can found the actual problem that is I was writing the above code on Jira API’s and I was storing the username and password in a string variable which is used inside auth().basic(“”,””).
Since the password are encrypted it was not allowing RestAssured class to post or access the API.
So I used http clients Base64 password encoding and made the post call.

String getting split on '#' when passing to a RESTful Web Service in Java through GET method

I am having a RESTful Java Web Service that accepts a long string with '#' in between.
When I am trying to send the string to the method while calling, the string is getting split on '#' and I can retrieve the [0] value alone.
Before sending the message is intact, but after using this..
req.open("GET","https://localhost:8443/registername/resources/registerName/"+"My#Name", true);
req.send();
is the problem.
These are the first few lines in the Web Service...
#GET
#Path("/{message}")
public String validateName(#PathParam("message") String message) throws Exception{
System.out.println(message);
...}
And, it displays "My" alone.
Can anyone please help me on why this is happening? Thanks!
In URLs, a # sign indicates a "named anchor," something that local javascript, and it is not sent to the remote server, so when you have the URL:
https://localhost:8443/registername/resources/registerName/My#Name
Name isn't sent to the server. You need to use a different split character.
See What is it when a link has a pound "#" sign in it or http://www.hypergurl.com/anchors.html for more information.
HTTP post or get will not read anything after #,
Do a URLEncode before doing POST or GET.
I don't think the part after the # is sent up with the GET request.

Invoking a 'REST' service which have query parameters in the URL

I have to invoke a GET on a service which returns text/xml.
The endpoint is something like this:
http://service.com/rest.asp?param1=34&param2=88&param3=foo
When I hit this url directly on a browser (or some UI tool), all's good. I get a response.
Now, I am trying to use CXF WebClient to fetch the result using a piece of code like this:
String path = "rest.asp?param1=34&param2=88&param3=foo";
webClient.path(path)
.type(MediaType.APPLICATION_JSON)
.accept(MediaType.TEXT_XML_TYPE)
.get(Response.class);
I was debugging the code and found that the request being sent was url encoded which appears something like this:
http://service.com/rest.asp%3Fparam1=34%26param2=88%26param3=foo
Now, the problem is the server doesn't seem to understand this request with encoded stuff. It throws a 404. Hitting this encoded url on the browser also results in a 404.
What should I do to be able to get a response successfully (or not let the WebClient encode the url)?
Specify the parameters using the query method:
String path = "rest.asp";
webClient.path(path)
.type(MediaType.APPLICATION_JSON)
.accept(MediaType.TEXT_XML_TYPE)
.query("param1","34")
.query("param2","88")
.query("param3","foo")
.get(Response.class);
You will need to encode your URL. You can do it with the URLEncoder class as shown below:
Please replace your line
String path = "rest.asp?param1=34&param2=88&param3=foo";
with
String path = URLEncoder.encode("rest.asp?param1=34&param2=88&param3=foo");

How to encode a URL that I want to pass as a query string

I'm trying to send a URL as paramter of a query string like this example:
http://localhost.com/myapp.jsp?pathToFileURL=http://192.168.0.1/my_file.pdf
What I did is I used encode URL to encode the path before sending it to the server, problem is im getting a "400 Invalid URI: noSlash" because of this.
From what I read the problem is the tomcat security and that I should add a parameter to the tomcat startup
-Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
But I can't modify the parameters of the tomcat, so is it possible to do it other way?
Thanks
You can do URLSafebase64 encoding at the client side and URLSafebase64 decoding at the server side.
Check URLEncoder class for more details:
http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html
You can test manually before coding using any of the online URL Encoder/Decoder. Just google for "URL Encoder/Decoder"
Complete stab in the dark but you could try escaping the slashes with backslashes or you could try replacing them with %2F which is the URL encoded version of forward slash.
Hope this helps.
Base64 the URL then on the receiving end base64 decode to get the original URL without any alteration

Categories

Resources