I'm trying to create a little test servlet (Tomcat 10) to accept simple "POST" messages, however no matter what I've tried the servlet never seems to get the body (json).
I'm using Postman to send the messages (http).
I've tried getReader() and getInputStream() and both keep returning nothing. I've read that this can happen when the buffer has already been read, but I'm not reading it twice (not that I am aware of).
Here are some debug values:
getContentLength() => -1
getContentType() => application/json
getReader().ready() => false.
So, either no body was recieved or it was already read - but where? I've been using Postman for other API's without issue, so I don't believe I've done something wrong there, what else can I try within the servlet?
Does anyone have any suggestions of what I could try to get this working?
Update
Thank you #parsifal, I tested the servlet using PowerShell's Invoke-WebRequest and it worked perfectly, so I took another look at all my Postman settings, tested several things and then finally found my error. The Header parameter Content-Length wasn't selected - I had incorrectly assumed that it would be automatically sent, but it wasn't.
Related
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?
I want to set a status code of 404 on outgoing response using Servlet Filter, if the status code was initially going to be 403. This is basically because I want to prevent any attacker from knowing about the existence of the resource.
I know this cannot be accomplished by simple HttpServletResponse - changing the response code inside if(status==403) condition after chain.dofilter() does nothing, because the response has already been commited.
I tried the code given in the answer here - Response is committing and doFilter chain is broken
But it gives me the following error:-
java.io.IOException: UT010029: Stream is closed
at io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:133)
at io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:125)
at CODE.filter.ResourceNotFoundFilter.doFilter(ResourceNotFoundFilter.java:76)
I think that the above answer includes more than I require, and I want to strip away the unrequired parts, but I'm not sure which those are. Also, I have multiple filters and this one has to be the last in the chain. Do I have to create a response wrapper from the start and pass that wrapper through all filters? Or can I use simple HttpServletResponse for all the filters before this one?
The error generated above is basically because of this line in the code. This is the 76th line in my file.
response.getOutputStream().write(wrapper.getWrapperBytes());
Expected result - I want to change the status code to 404 if it was initially going to be 403, using Filters.
Actual result - I can't get it to work.
Please help.
I have a HTTP GET which returns status 200 with some response. This response it is given as a result of redirecting.
If I introduce asyncHttpClient.setEnableRedirects(false); in my code, then redirection stops and it comes in failure with status 302. But in my application, this status 302 and response associated with it is what I need.
I am searching online and trying to figure out, but I am new to Java so not able to understand how to achieve this.
What I want is, when server returns status 302, I want to trigger onSuccess and capture response. Thanks.
If I'm getting right what do you need then you could implement your own AsyncHandlers to react with a different AsyncHandler.State for 302 status code. ( https://github.com/AsyncHttpClient/async-http-client#using-custom-asynchandlers) or while creating the client stop to follow redirects like this:
asyncHttpClient(config().setFollowRedirect(false))
(it might be different from the approach you mentioned above.)
P.S. I'm using 2.5.2 version of the asynchttpclient lib.
when I raise request with follow url:
myip/searchapp/genericSearch/genericSearchInit.html?securityName=&identifierType=3&identifierCode=test&vendor=4&startDate=10%252F1%252F2013&priceType=¤cy=&endDate=&exchange=
I got 400 error:
Failed to load resource: the server responded with a status of 400 (Bad Request)
But when I give some values such as
myip/searchapp/genericSearch/genericSearchInit.html?securityName=&identifierType=3&identifierCode=test&vendor=4&startDate=10%252F1%252F2013&priceType=1¤cy=1&endDate=1&exchange=1
and it become legal url.
can any guy tell me why? and how can I deal with it?
Try leaving out those last parts completely if you don't need them:
myip/searchapp/genericSearch/genericSearchInit.html?securityName=&identifierType=3&identifierCode=test&vendor=4&startDate=10%252F1%252F2013
it must be that whatever page you are calling is using those variables to redirect to different pages. It seems that the page breaks when an empty string is sent in.
That page is probably using key_exists() checks, which would return false if the query didn't have it at all but true if you sent an empty string, and not an additional empty() checks, which would return false either way.
If that page is yours, then you could probably adjust it so that the it is valid to send empty data. If it isn't yours then it is just the way that specific page is working, there really isn't anything else to understand.
It is a bug, report it to whoever is in charge of the page.
The only difference that I can tell in those two urls is
priceType=¤cy=&endDate=&exchange=
versus
priceType=1¤cy=1&endDate=1&exchange=1
It seems the server doesn't like missing data and therefore sends 400 Bad Request. You need to figure out what your service expects and send that.
Sorry if this is a duplicate question but google isn't smart enough to understand me or I'm too dumb to express my question simple enough for it to understand.
I don't know if this is my problem but I'm 90% sure this is it.
I'd like to know how to represent a Unix path within a GET request so that my web service doesn't return a 404. I think it's because one of my JSON fields in the query is a Unix path and because of the slashes, the webservice thinks it's part of the URL and not a part of my query.
For example, I'm using a RESTClient that's an add-on to Mozilla to test my web service. For the POST request, I enter as the url
http://mytestserver:8080/mydir/
and in the body, I put in my JSON object
{"filename":"obit.jpg", "Path":"test/2/1"}
This method works fine. I get a status code 200 and a return JSON object with the expected output.
When I use the same string for a GET request, I get a status code 404 and no return JSON object. I put as the url in the RESTClient
http://mytestserver:8080/mydir/{"filename":"obit.jpg", "Path":"test/2/1"}
and I get a status code 404 and the response body just says 404 - Not found
To further test my theory, I entered the following url in a GET request, removing the /2/1 from the path, which works.
http://mytestserver:8080/mydir/{"filename":"obit.jpg", "Path":"test"}
I've tried encapsulating the whole JSON string in quotes but that didn't work either so I've run out of things to try.
Thanks in advance for any help you can give me. If I need to show some code, please let me know, although, I don't think it's a code problem, I think it's a representation problem. Thanks.
Found out that JSON objects are usually sent via POST, not GET. Since I appended it to the URL via GET, it gave me problems. Per How to send a GET request with a "/" in the query