Java http servlet get data inside -d parameter - java

I am sending a curl to a web app like
culr -X POST 10.1.2.7?myname -d '&test=111'
I am using parameter.getQueryString to get myname. I need to get that value in parameter test.

Try to use this in your servlet:
String test = request.getParameter("test");

Related

Converting Curl to POST api for java

I would like to post some data to a rest api.
The API documentation (See page 38) asks for the following:
curl -u "USERNAME:PASSWORD" -H "Content-type: text/xml" -X "POST"
--data-binary #-
"https://qualysapi.qualys.com/qps/rest/3.0/create/was/webapp/" <
file.xml
Note: “file.xml” contains the request POST data.
Request POST data:
<ServiceRequest>
<data>
<WebApp>
<name><![CDATA[My Web Application]]></name>
<url><![CDATA[http://mywebapp.com]]></url>
</WebApp>
</data>
</ServiceRequest>
I have confirmed that the call works on the command line using curl.
I then began to write a small app in Java and found UniRest.
Thats where the problem starts. I do not know how to convert the curl request into Unirest.
I have this much so far:
Unirest.post("http://apiurl).basicAuth("user","pass").field(name, file).asBinary();
the latter half
.field(name, file).asBinary();
doesnt make sense to me. What is the intent behind giving the file a name. Isn't suppose to retrieve the data from the file?
Furthermore, I would like to avoid writing data to file. How can I create the same xml with UniRest.
If not xml, could I do the same with JSON? The API attached above (appendix C) also accepts JSON. However, how can I nest fields with the builder pattern of the Unirest api
According to the UniRest documentation, it looks like you can write any array of bytes to a field in the request. You just have to encode the string into a byte array.
Unirest.post("http://apiroot")
.field(name, xmlString.getBytes(StandardCharsets.UTF_8))
.asBinary();
Alternatively, you can use any InputStream,
Unirest.post("http://apiroot")
.field(name, new CharSequenceInputStream(xmlString, StandardCharsets.UTF_8))
.asBinary();
Usually data is the body of the request though (not as a field). If you do want to send the data as the request body and not a form field, you should use the body(String body) method instead of the field(String name, Object object) method, for instance:
String data = "<ServiceRequest>... etc...</ServiceRequest>";
Unirest.post("http://apiroot")
.body(xmlString)
.asBinary();

How to extract parameters from curl request in jersey servlet?

I am making a curl post restful request to my jersey servlet in the form
curl -i -X POST -d "debit_user_id=/custome/mobile_number:917827448775"http://localhost:8080/switch/apikongcall.do/transactions
I need to fetch the debit_user_id in my servlet, code for my Post method is
#POST
//#Path("/transactions")
//#Consumes(MediaType.APPLICATION_JSON)
public Response createTrackInJSON(#QueryParam("debit_user_id") String debit_user_id) {
//Log logger = null;
this.logger = LogFactory.getLog(getClass());
this.logger.info("Inside post method"+debit_user_id);
String response = debit_user_id;
//String response = "testParam is: " + recipient_id + "\n";
//String result = "Track saved : " + track;
return Response.status(200).entity(response).build();
But my debit_user_id is coming as null. Is it the correct way to make the curl restful request or the way I am extracting it in my servlet is wrong.
I am new to jax-rs. Thanks in advance for the help.
The -d option to curl passes in a url encoded form parameter. You have to change #QueryParam to #FormParam to make the given Curl command work. Also, just specify the parameter name as mobile_number without the pathing that you used in you curl command, like so:
curl -i -X POST -d "debit_user_id=mobile_number:917827448775" http://localhost:8080/switch/apikongcall.do/transactions
maps to
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createTrackInJSON(#FormParam("mobile_number") String debit_user_id) {
...
}
If you do in fact want a query parameter, your curl command would need to change:
curl -i -X POST http://localhost:8080/switch/apikongcall.do/transactions?mobile_number=917827448775
For security reasons, it's probably better to keep the mobile number in the message body, so I'd use the FormParam instead of the QueryParam.

How to extract parameters from a restful post request using jersey and convert it into parameterized form? [duplicate]

I am making a curl post restful request to my jersey servlet in the form
curl -i -X POST -d "debit_user_id=/custome/mobile_number:917827448775"http://localhost:8080/switch/apikongcall.do/transactions
I need to fetch the debit_user_id in my servlet, code for my Post method is
#POST
//#Path("/transactions")
//#Consumes(MediaType.APPLICATION_JSON)
public Response createTrackInJSON(#QueryParam("debit_user_id") String debit_user_id) {
//Log logger = null;
this.logger = LogFactory.getLog(getClass());
this.logger.info("Inside post method"+debit_user_id);
String response = debit_user_id;
//String response = "testParam is: " + recipient_id + "\n";
//String result = "Track saved : " + track;
return Response.status(200).entity(response).build();
But my debit_user_id is coming as null. Is it the correct way to make the curl restful request or the way I am extracting it in my servlet is wrong.
I am new to jax-rs. Thanks in advance for the help.
The -d option to curl passes in a url encoded form parameter. You have to change #QueryParam to #FormParam to make the given Curl command work. Also, just specify the parameter name as mobile_number without the pathing that you used in you curl command, like so:
curl -i -X POST -d "debit_user_id=mobile_number:917827448775" http://localhost:8080/switch/apikongcall.do/transactions
maps to
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createTrackInJSON(#FormParam("mobile_number") String debit_user_id) {
...
}
If you do in fact want a query parameter, your curl command would need to change:
curl -i -X POST http://localhost:8080/switch/apikongcall.do/transactions?mobile_number=917827448775
For security reasons, it's probably better to keep the mobile number in the message body, so I'd use the FormParam instead of the QueryParam.

Query parameter not being extracted - JAX-RS and Jersey

I'm using Jersey 2.19 to implement a REST API but I'm having difficulty using #QueryParam to extract the query parameters from a POST request even though my resource method is being called.
This is my resource method:
#POST
#Produces(MediaType.TEXT_PLAIN)
public Response test(#QueryParam("test-param") String testParam)
{
String response = "testParam is: " + testParam + "\n";
return Response.status(Response.Status.OK).entity(response).build();
}
I'm using cURL to submit the HTTP POST request as follows:
curl -X POST http://192.168.0.2:8080/myApp/test --data test-param=Hello
The value returned is always null.
What am I doing wrong?
The --data in curl will provide the whole text test-param=Hello. The correct way to request it is:
curl -X POST http://192.168.0.2:8080/myApp/test?test-param=Hello
try to use curl -X POST '192.168.0.2:8080/myApp/test?test-param=Hello';
-d, --data
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.

Get data in java when http request sent by curl command

Does anyone know how to get data in java when http request sent by curl command "--data"?
For example :
curl --data { Name : username , Gender : gender , Age : age } -X PUT http://localhost:8080/user/folder -v
I want to know how to get data { Name :username ,..... age } using curl command --data.
I use the method in REST Web service which is used jersey framework and java .
Since you PUT data when you use curl this way, all you need to do is to implement a PUT request handler. In theory at least :)
Something like
#PUT
#Path("/user/folder")
#Consumes("application/json")
public void receiveData(String data) {
......
}
edit: didn't see the -X PUT at first. you want a PUT handler not a POST handler.

Categories