Url in path param - java

in jax-rs is it possible to send path param like this "http://localhost:8080/"?
I mean there is no problem when I want to send path param like "1" or "6". But how can I send this? "http://localhost:8080/"
#Edit
For example I've path like this:
/api/media/{url}
and I want to transfer http://localhost:8080/ under {url}

You can pass it by encoding all special characters using percent encoding. Recommended way is to pass as query parameter with percentage encoding

Related

Encoding a file path as query parameter with Retrofit2 doesn't work

I'm trying to encode a file path in a query parameter, but I have always the same output.
I've tried:
#Query(value="path", encoded=false) String path
and
#Query(value="path", encoded=true) String path
but the request looks like this in both cases:
/myendpoint?path=C:\Development\some\random\filepath
I expect the following output:
/myendpoint?path=file:%5C%5C%5CC:%5CDevelopment%5Csome%5Crandom%5Cfilepath
if this is not possible it would be nice to get this output:
/myendpoint?path=C:%5CDevelopment%5Csome%5Crandom%5Cfilepath
so I can add the "file:\\" string to the file path before passing it to my retrofit method, but the Url-encoding should be done by retrofit and the path should be sent as query parameter!
Do anybody know how to get this work?
Edit:
As I found out Retrofit has the following behaviour for query-encoding:
if the query contains invalid characters (not encoded, yet) encoding doesn't work
if the query contains valid characters (already encoded) encoding works (encoded parameter turns encoding on and off)
Thank you

how request.getparameter() take special characters like #,%,& etc

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

java servlet: request parameter contains plus

The request parameter is like decrypt?param=5FHjiSJ6NOTmi7/+2tnnkQ==.
In the servlet, when I try to print the parameter by String param = request.getParameter("param"); I get 5FHjiSJ6NOTmi7/ 2tnnkQ==. It turns the character + into a space. How can I keep the orginal paramter or how can I properly handle the character +.
Besides, what else characters should I handle?
You have two choices
URL encode the parameter
If you have control over the generation of the URL you should choose this. If not...
Manually retrieve the parameter
If you can't change how the URL is generated (above) then you can manually retrieve the raw URL. Certain methods decode parameters for you. getParameter is one of them. On the other hand, getQueryString does not decode the String. If you have only a few parameters it shouldn't be difficult to parse the value yourself.
request.getQueryString();
//?param=5FHjiSJ6NOTmi7/+2tnnkQ==
If you want to use the '+' character in a URL you need to encode it when it is generated. For '+' the correct encoding is %2b
Use URLEncoder,URLDecoder's static methods for encoding and decoding URLs.
For example : -
Encode the URL param using
URLEncoder.encode(url,"UTF-8")
Back in the server side , decode this parameter using
URLDecoder.decode(url,"UTF-8")
decode method returns a String type of the decoded URL.
Allthough the question is some years old, I'd like to write down how I fixed the problem in my case: the download link to a file is created in a GWT page where
com.google.gwt.http.client.URL.encode(finalurl)
is used to encode the URL.
The problem was that the "+" sign a customer of us had in the filename wasn't encoded/escaped. So I had to remove the URL.encode(finalurl) and encode each parameter in the url with
URL.encodePathSegment(fileName)
I know my question is bound to GWT but it seems, URLEncoder.encode(string, encoding) should be applied to the parameter only aswell.

url encoding skipping the fqdn

I have a question regarding url encoding. Trying to encode the url and could not get it working. Tried java.net.URLEncode.
I have url http://msnbcmedia4.msn.com/i/MSNBC/Components/Photo/_new/130409_luke hancock.jpg and I need to encode it. From online forums my understanding is that I should only encode queryparams and url path excluding fqdn(http://msnbcmedia4.msn.com). Should I need to encode(/ in url path, ? and & in parameters) or skip encoding these. I am trying to download the content from this specific location using java. Any info would be appreciated.
URLEncoder is the right choice. You need to encode only individual Query string parameters name/value and not the entire URL. If you encode whole URL then it will encode Http and other URL parts as well which we don't want.
Check out this awesome answer >> https://stackoverflow.com/a/10786112/2093375
Regards,

JSR168 portlet request cuts param value after # symbol

One of parameters for action link looks like:
itemUrl=feedLink.html#xtor=RSS-3208
when I execute next code in backend in processAction():
String itemUrl = (String) request.getParameter("itemUrl");
,that I get next value: feedLink.html
e.g. request cuts itemUrl value after # symbol
escapeXml="true" in .jsp file doesn't help.
You have to URI encode the parameter names and values - your link should be itemUrl=feedLink.html%23xtor=RSS-3208.
Anything after the # in an URL specifies the location on the page the browser should display; it's not part of the URL itself. As such, if you want an actual # in your URL, it needs to be escaped (if the parser is actually compliant).
In theory, you could parse the whole URL being sent to you manually, but the better solution is to get the caller of your page to send you a correct URL in the first place (well, a URL that represents what they want, since the one in question is valid, per se).

Categories