Java: Getting parameters and body from a HTTP POST - java

I want to read post parameters and the body from a http post.
Example:
If you post to the url: http://localhost/controller?sign=true.
In the post there is also json data in the body.
{"transaction":
{"user":[
{"name": "Anna"}]
}
}
Getting the parameter is done via
public java.lang.String getParameter(java.lang.String name)
And the body can be retrieved via
public ServletInputStream getInputStream() throws java.io.IOException
But how do you get in hold of both the parameter and the body?
Because if i call getParameter before getInputStream the result will be -1 on the inputStream.

I believe under the covers of getParameter(String name), the ServletInputStream is being read to get those parameter. If you're going to be mixing POST data with URL parameters (I'm assuming the sign=true is the parameters you mentioned trying to get) use HttpServletRequest.getQueryString() to get the URL parameters, then you should still be able to read the body with getInputStream(). You will probably have to parse through the query string to get the information you're looking for, however.
EDIT: I forgot to add in my original answer that when the ServletInputStream is read, it cannot be read again. If the data from the stream needs to be used multiple times, you'll have to store it.
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

Related

How to get response body of GET call in RestAPI?

Am new to API testing. I wanted to validated the response body of the GET method. But it is returning the io.restassured.internal.RestAssuredResponseImpl#35adf623 with the below code. Please let me know how can I resolve this. With POST Method, it works fine. Failing with GET method provided am passing all other values correct.
public static Response getResponseWithGetMethod() throws Exception {
Response response = RequestInvoker.invokeGET();
return response;
}
Output :
io.restassured.internal.RestAssuredResponseImpl#35adf623
Expected output is :
{
"path1": true,
"path2": true,
"path3": true
}
Use the body() method to get access to the body of the response.
Response object has multiple fields in it like, body, headers, status code, cookies etc.,
Check out the complete java doc here.
Answer to your code is to call getResponseWithGetMethod().body() or getResponseWithGetMethod().asString(); later one might be appropriate for you.

Servlet get GET and POST's parameters at the doPost method

My problem is when I'm trying to access a POST Variable with request.getParameter("name"), it works perfectly. But in some conditions, when a POST request arrives at my application, I also need to get GET Parameter from the Query String.
As far as I can see, with getParameter, you can only access current request's parameters, but, as in my condition, as I said, I also need to fetch GET Parameters inside doPost method.
Is there a way to fetch GET Parameters without parsing the Query String?
If you have parameters with the same name in the query string and in the posted form data, use getParameterValues().
Example:-
String fromQuery = request.getParameterValues("name")[0];
String fromForm = request.getParameterValues("name")[1];
The getParameter() method can return (if possible) both GET and POST parameters as it works transparently between GET and POST. You don't need to do any explicit work to get the GET parameters. you can use getParameter for both query parameters and POST parameters.
But should you do it? - It's considered a poor design practice especially if there is sensitive information to be sent.
Take a look at this answer:
HTTP POST with URL query parameters -- good idea or not?
I think you have a confusion here. You can retrieve all the request parameters (in both GET or POST or others) using the same getParameter(..) depending upon the type of request. If it's a GET request, you can retrieve all the GET parameters.
If it's a POST request, you can retrieve all the POST parameters. You get parameters using getParameter(...). And you make one request at a time. If you make a POST request in html or JSP file, you use doPost method receive all the parameters. At this point, there is nothing in GET request. Then after that, you make a GET request, you retrieve all the parameters in doGet method. At this moment, there is nothing in POST. Remember, HTTP requests are stateless.
To complete #Rei answer check out this code :
your form
<form action="?nom=nom1">
<input type="hidden" name="nom" value="nm2"/>
your doPost
System.out.println(request.getParameter("nom"));
String s = "";
for(String ss : request.getParameterValues("nom")) {
s += "|" + ss;
}
System.out.println(s);
System.out.println(request.getParameterMap().get("nom"));
what will be printed
nom1
|nom1|nm2
[Ljava.lang.String;#c7068db
ps : thanks to Julien for the code and testing

Create a HTTP GET request which sends JSON data in body with Scala?

I am having a problem when i integrate API of our partner. They always use Json data in body of HTTP request for both "POST" and "GET" method.
I'm using scalaj library for creating HTTP request but it can not send json data in Body for GET request. when i put json data to body, the Method will change to POST automatically.
I also tried to use default HttpUrlConnection in Java, but it have same problem. When i call setDoOutput method, HttpUrlConnection will change Method to POST.
How can I solve this problem in Scala?

Reading Jira Webhook POST data

I am trying to read Jira issue data using a webhook that posts the data to my servlet.
When I travserve the request parameters map, I don't find anything in it.
But the content lenght shows as "8876" which means webhook is sending the data. Somehow I am not able to read/retrieve the data in my servlet.
Also checked, content-type returns as "application/json".
Does anyone know how to read Jira webhook post data?
You have to read the response body, not the parameters map. For that purpose you can use
request.getInputStream();
or
request.getReader();
method.
PS: You can configure the web hook to post data to http://requestb.in/ so you can easily analyze the request parameters, the request body, the headers, etc.

Java - send HTTP POST request without downloading all the content

Is it possible to send HTTP POST request to a webserver and retrieve just headers of response or read just few bytes of the body, so the rest won't be downloaded at all (so it won't consume traffic)? If yes, how?
I know that there is a HEAD method for this, but I need to achieve it by POST method .. well, I am not sure if I need the POST method, I just need to post the data. Maybe if the webserver isn't secured well enough (it doesn't check what method it's used - it's just directly access the post data), is it possible to send "post data" by HEAD request?
There is no built-in HTTP mechanism for this, and HTTP HEAD requests do not allow content in the body. If however you are the one writing the server code then anything is possible.
If this is the case, I would suggest a URL parameter that triggers this behavior. For example:
POST /myURL - This would return the whole response
POST /myURL?body=minimal - Returns the reduced size response that you are looking for.
And you would have to code your server method to construct and return the appropriate response based on the URL parameter.

Categories

Resources