400 bad request after URL encoding - java

I am consuming REST web services in my java code. Before make call to Rest web service I am encoding my query parameters by java.net.URLEncoder
But by request is getting failed by 400 bad request exception
URL before encoding : host?limit=200&filter=published ge "2017-12-15T16:50:34.034Z" and (action.objectType eq "core.user_group_member.user_add" or action.objectType eq "core.user_group_member.user_remove")
URL after decoding : %3Flimit%3D200%26filter%3Dpublished%20ge%20%222017-12-15T16%3A50%3A34.034Z%22%20and%20%28action.objectType%20eq%20%22core.user_group_member.user_add%22%20or%20action.objectType%20eq%20%22core.user_group_member.user_remove%22%29
I am tried same from postman rest client tool there also without encoded url is working and encoded url failed with 400 bad request error.

"limit=200&filter=" doesn't need to be coded.

In postman you need to encode manually relevant parameters' values as follows:
Right click on a piece of selected text, and select “EncodeURIComponent” to manually encode the parameter value.

Related

Getting the junk values in the URL - RESTAssured

I'm automating my GET API with RESTAssured, while I see the URI just before hitting the API, it is just normal, but when I add the query parameters, it adds junk values. I don't understand why adding query params does so, even when I print query params, it gives me proper value.
Here is my code snippet.
System.out.println("QUERY PARAMS"+queryParams);
System.out.println(">>>>"+uri);
Response response = RestAssured.given().config(RestAssured.config().sslConfig(this.getSslConfig())).filter(new AllureRestAssured()).contentType(appJsonContentType)
.headers(requestHeader)
.queryParams(queryParams)
.when()
.log()
.all().get(uri);
And here is the logs -
QUERY PARAMS{amount=[2000.0], currency=[USD], date=[2022-04-01T00:00:00Z], id=[413275]}
>>>>https://www.example.com/amount/21345699
Request method: GET
Request URI: https://www.example.com/amount/21345699?amount=2000.0&currency=USD&date=2022-04-01T00%3A00%3A00Z&id=413275
I'm not sure why I'm getting this %3A in my URL. I tried
.all.get(URLEncoder.encode(uri, StandardCharsets.UTF_8));
but it adds some localhost:8080 ahead of the URL. Can someone please help?
These are URL encoded characters, as not all the character can be written directly to URL.
%3A is URL encoded sign ":" which I assume is separating fields of hour:minute:second in your parameters

How do I modify the HTTP response code and message in java

My goal is to modify the http response status code and the content that comes with it (not using spring by the way). The current response I'm getting is an HTTP 501 and I would like to modify that response to return an HTTP 200 instead with a custom message.
I've tried using javax.servlet.Filter and found out that that won't work (as far as I know) since I can't modify the response after the filterChain.doFilter() call. I tried redirecting a response using httpResponse.sendError() but didn't work since I'm getting an IllegalStateException telling me about the reponse was already committed or something like that.
I've also tried adding an <error-page>...</error-page> block in my web.xml using a web servlet as the location but apparently that does not work for unsupported methods and invalid headers (tried using a random string as request method; also tried Transfer-Encoding: random as an invalid header); works for invalid uri paths though.
Is there a way to create a general "filter" mechanism where I can modify the response code and message when there's a HTTP error code response?

Add %% in a url query parameter

I am forming a url that contains another url as one of its query string parameters. This url is supposed to return some xml code from our webapp deployed on tomcat.
Below is the sample url that I am generating using Java:
http://localhost:8080/sample/url?id=1&uid=sfdgsdh&nurl=https%3A%2F%2Flocalhost%3A8080%2Fxmltag%2Freturn%3Fzid%3D1_0_1%26sid%3Dfdd61fce-29b4-11e7-9ac0-eb0a8802439b%26au_pc%3D%25%25TAG_PRICE%25%25
Now the issue is when I hit this url from browser or wget it, it returns 'ERROR 500: Internal Server Error'. But if I replace the last macro '%25%25TAG_PRICE%25%25' with some real value e.g. 5.2, then this url returns valid response.
So I assume the issue is with passing %%TAG_PRICE%% in the query string. Its not working no matter its encoded or not and I need to have this macro in my url. I tried to debug it using eclipse debugger, but its not even reaching init method of web servlet. I don't know how to fix this.
Any help is appreciated. Thanks in advance.
Finally I figured out that if I encode the macro %%TAG_PRICE%% twice, it works fine i.e. if I insert %2525TAG_PRICE%2525 as a query parameter in the above url, browser successfully loads its content.

Extracting ViewState when testing JSF with JMeter

I'm using JMeter to do some load tests on my JSF application and I'm having trouble passing the ViewState along the pages. The ViewState variable doesn't get extracted at all or it doesn't get passed along the pages.
I've recorded my test steps with a proxy server and this is what it looks like:
I've added the Regex extractor in the first GET request. Tested the regex and it is correct.
In every POST request I replace the hardwired View IDs with my variable.
And what I get when I send the request is the following:
The POST parameters are incorrect, as it sends the name of the variable.
POST data:
loginForm%3ArequestToken=&loginForm%3Ausername=heller&loginForm%3Apassword=%21QAYxsw2%A7EDC&loginForm%3AloginButton=Anmelden&com.sun.faces.VIEW=%24%7BjsfViewState%7D&loginForm=loginForm
Could you tell what I'm doing wrong here?
Thanks!
The ViewState parameter is an encoded value (Base64 I believe?) and may contain values that would be inappropriate if passed in a GET request through the url. URL parameters are typically encoded so that special values (Eg. space -> %20) can be represented and decoded when the request reaches the server.
The issue here is that the following request is a POST meaning that the parameters do not need to be URL encoded.
com.sun.faces.VIEW=%24%7BjsfViewState%7D&loginForm=loginForm
The above shows that JMeter or some other process is URL encoding the ViewState in the request which is incorrect. The value of the ViewState should simply be sent as is.
Found my problem: the regex was wrong, so it couldn't find anything in the response. I had to change the regex. Noticed it after adding a default value "NOT FOUND".

Server returned HTTP response code: 400

I am reading data from a webservice. The issue if I put the link on the browser it works fine. When I run like this give me error. I am suspecting is it due to the way how I send my parameters. My paramater list has this dID=1,5,7,11,14,18,26&FromDate=18 Sep 2012 00:00 am&ToDate=18 Sep 2012 10:00 am. Do I need to do some encoding here?
URL xmlURLDM = new URL(urlDM);
InputStream xml2 = xmlURLDM.openStream();
I get this error
java.io.IOException: Server returned HTTP response code: 400 for URL:
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1612)
at java.net.URL.openStream(URL.java:1035)
at xmlreader.main(xmlreader.java:172)
You do need encoding, most likley it is the spaces in your URL that is causing the trouble.
Use Javas built in url-encoding. eg:
String encoded = URLEncoder.encode(myUrl, "UTF-8");
...
call web service with encoded as URL
There can be other reasons for the status code being 400, but this encoding issue is probably your first stumbling block.
The Documentation of URL says,
The URL class does not itself encode or decode any URL components
according to the escaping mechanism defined in RFC2396. It is the
responsibility of the caller to encode any fields, which need to be
escaped prior to calling URL, and also to decode any escaped fields,
that are returned from URL. Furthermore, because URL has no knowledge
of URL escaping, it does not recognise equivalence between the encoded
or decoded form of the same URL.
So please use URLEncoder.encode() before you invoke URL()

Categories