only space in URL of a GET request in Rest causes 404 - java

Team,
I have created a search API and made it as GET request. Now when I try to give only a single space in search, it gives me 404 resource not found error. For example:
http://localhost:8080/myproject/myapp/search/ ?pageNo=1&limit=20
As you can see there is a space in the URL after /search/. This URL returns me the error 404. I'm using Spring 4.x version.
Thanks

The URL is invalid with a space between the URL part and the request parameter string '?'. You need to encode the URL to replace characters like space with '%20'.
Try:
String encodedUrl = URLEncoder.encode(url, "UTF-8");

You can also trim all spaces before your query goes to request URL. Not sure that user need to have ability to search by space (nothing).
And using of URLEncoder for strings coming to URL is very recommended.

Related

When I'm using URLEncoder for Http GET Request I'm getting proper URL but when I push that code to live or on server I'm getting error

When I'm using URLEncoder for Http GET Request I'm getting proper URL but when I push that code to live or on server I'm getting error.
The reason is in live or server more than 3 spaces are converted into %09 ie. 5 spaces become +++%09
hmMailDetails.put("content", URLEncoder.encode(sContent.trim(),"UTF-8"));
Just i have replaced the %09 with an %20........
String cont = URLEncoder.encode(sContent.trim(),"UTF-8");
hmMailDetails.put("content",cont.replace("+", "%20").replace("%09", "%20"));

Malformed Url in case of woocommerce api through android

The "woocommerce api generate signature" url is
String getUrl = GET&http%3A%2F%2Fprojectrepo.net%2Fscoop%2Fwp-json%2Fwc%2Fv2%2Forders&oauth_consumer_key%3Dck_2f53925c....6407f09f67f5f118d01ed80e%26oauth_signature_method%3DHMAC-SHA1
When the url is passed through the below method,
URL url = new URL(getUrl);
"malformed url" error is shown.
And when the "GET&" is ommited from the url,this error is gone.But the "GET&" is needed for signature generation in woocommerce api.
What is the problem? If any doubt,please comment.
Thanks for your time.
Well the "GET&" characters before the url is mandatory for generating signature.When the actual posting of data is done the "GET&" characters should be ommited.
Everyone thanks for your time.

POST String as parameter in Url

I am testing foursquare api. Here is the url.
https://api.foursquare.com/v2/checkins/add?venueId=123&shout=Test&broadcast=public,twitter,facebook&oauth_token=something
the problem is when I add shout="test". It works fine. But "test message" isn't working. I mean test[space]message isn't working.I tried with removing spaces,it worked.But,that can't be solution. How can I handle such thing I mean how can I send post request with shout="test messgae" with space include.
Try using shout="test%20message"
%20 is just HTML code for a space
This is another post found on Stack Overflow that provides more detail Encoding URL Query Parameters

Getting unescaped servlet path from a HttpRequest object

I have a custom proxy servlet that has to deal with URL-s that contain special characters (e.g. ; , . / in their) in their path. This is because it is a RESTful application that has ugly path params by design. (Don't comment it as it is not mine.)
My client, (actually wget, because browsers tend to show unescaped the URL) send a request to this URL:
http://localhost:8080/MyApplication/proxy/foo/ugly%3Apart%2Fcomes%3Bhere/children
//note: %2F = '/', %3A = ':', %3B = ';'
In my servlet (mapped to /proxy/*) when I try to forward the GET request, I am unable to reconstruct it because HttpRequest.getPathInfo() returns me the URL unescaped:
http://localhost:8080/MyApplication/proxy/foo/ugly:part/comes;here/children
And therefore the information of which /s and ;s were originally escaped or unescaped is lost. And that makes a difference for me, for example ; makes my URL a so called matrix URL, see http://www.w3.org/DesignIssues/MatrixURIs.html, or all the REST path parameters get shifted by slashes.
Actually I found this issue on a Glassfish server, so I'm not sure if different application servers treat this differently or not. I found only this in the Servlet API:
getPathInfo() Returns any extra path information associated with the
URL the client sent when it made this request.
How could I get the original, unescaped request URL that was sent by the client?
Have a look at HttpServletRequest's getRequestURI() and getRequestURL() methods.
If you need to remove context and servlet mappings, look at getContextPath() and getServletPath().

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