I have this URI:
http://IP5:port/notification/myServlet.do?method=myMethod¶m1=[08]&method2=anotherParam¶m0=[07,04,06]
I want to obtain the URI's last part in the same order:
method=myMethod¶m1=[08]&method2=anotherParam¶m0=[07,04,06]
But' I can't discover how:
I can see in the request the field input, in order to do a substring after of ? character...
the methods: request.getRequestURI() and request.getRequestURL() is not working for me.
It looks like request is an HttpServletRequest. If so, you should be able to get what you are looking for using request.getQueryString().
Related
I have an HTTP GET request controller endpoint where I take in a fileName as a query param and pass that on to another service. For this request the param the filename could include any sort of special characters and I would like to keep these values encoded when passing them on. 2 Characters that have been causing issues are spaces (%20) and +(%2B).
How can I keep these characters encoded in the request params.
So far I have tried using the #RequestParam annotation as well as retrieving the params via HttpServletRequest.getParameterValues(String) but both return the decoded values as spaces.
Any help is appreciated thanks!
Yes, these are automatically decoded by the servlet API. You should be able to re-encode them -
encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8);
I found out that I could get the actual value passed in by using the HttpServletRequest.getQueryString() method. Parsing this query string I was able to get the un-decoded version of the fileName being passed in. I hope this helps someone in the future.
I have a request as follows:
localhost:8000/location/:01
My code takes as input an HttpContext request.
func(HttpExchange r) {
String area_path = r.getRequestURI(); // Equals string "/location/"
}
How do I parse an HttpExchange correctly so I can pull out the "01" from this path and store it as a variable?
That (localhost:8000/location/:01) is not a valid URL or URI
A plain colon character is not legal in the path of a URL or URI. If you want to put a colon in the path, it must be percent-encoded. Furthermore, if this was a URL, it would start with a protocol; e.g. http:.
Now ... it is unclear what the HTTP stack you are using will do with a syntactically incorrect URL / URI, but it could simply be ignoring the colon and the characters after it.
Your code looks a bit odd too. You have tagged the question as [java]. But the code looks like JavaScript rather than Java; i.e. func is a Javascript keyword. But it also looks like you are using the (deprecated) com.sun.net.httpserver.HttpExchange Java class. I don't know what to make of that ...
My advice:
Don't use a colon character in the URL path.
If you must do it, then percent-encode the colon it.
If you cannot encode it properly, then you may need to find and use a different framework for your HTTP request handling. One that will accept and handle a malformed URL / URI in the way that you want. (Good luck finding one!)
Unfortunately, the details in your question are too sketchy to give more detailed advice.
I pass some parameters in ajax URL and want to get that parameters by request.getParameter(); in controller if that parameters have some special character like #,%,&, etc. then how to get it?
String xyz = new String(request.getParameter("XYZ").getBytes("iso-8859-1"), "UTF-8");
You have two options:
1.Encode values to JSON before sending, and decode them on server.
Use javascript method encodeURIComponent https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
I found best solution after spending couple of hours use
((String[])request.getParameterMap().get("paramname"))[0]
which gives me param value with special charater
I am required to pass an arithmetic operation like 2+8 to a restful back-end and receive the result. I know that a simple operation can be handled in frontend using javascript, but i just want to follow the requirement.
I send the operations with the following uri:
http://localhost:8080/?question=2+5
and in the back-end i have:
#RequestMapping("/")
public String getAnswer(#RequestParam("question") String question){
System.out.println("recieved question is: "+question);
return botService.Evaluator(question);
}
When i print the question it is like 2 3 so there is no operation there.
And the component complains with:
javax.script.ScriptException: <eval>:1:2 Expected ; but found 5
2 5
^ in <eval> at line number 1 at column number 2
So, why the + is missing?
and how can i fix it?
Use the URLEncoder class to make sure any special characters are encoded safely for transport.
You need to encode the '+' symbol as the server will just translate it as a space.
Send...
http://localhost:8080/?question=2%2B5
You need URL Encoding here. for more details regarding URL Encoding please have a look in below link.
URL Encoding
I am doing an application where I have to read a URL from a webpage as a String[Its not the address of the page]. The URL that I will be reading contains query string, and I specifically need two queries from that URL. So I am using the Uri class available in Android. Now, the problem lies in the encoding/format of the URL and the query. One of the queries that I need is always an URL. Sometimes the query URL is %-encoded and sometimes not.
The URLs can be like the following :
Case 1 :
http://www.example.com/example/example.aspx?file=http%3A%2F%2FXX.XXX.XX.XXX%2FExample.file%3Ftoken%3D9dacfc85
Case 2 :
http://www.example.com/example/example.aspx?file=http://XX.XXX.XX.XXX/Example.file?token=9dacfc85
How do I get the correct Url contained in the file= query?
I am using the following [to accomplish the said work universally] :
Uri.decode(urlString.getQueryParameter("file"));
Is this the correct way to do it?
UPDATE
I have decided to first encode the whole URL regardless of its value and then get the query parameter. Theoretically, it should work.
If you are uncertain about the type of URL you would get then I would suggest you to decode every URL you get from the parameter. And when you need to use it then you can encode it.
As per my knowledge, you are doing it right.