Get data in java when http request sent by curl command - java

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.

Related

Spring rest with Spring Boot: Upload MultipartFile and Json object as parameters

I know this question was asked on stackoverflow before. However, I haven't found an answer that actually tests the service using, for example, Postman.
I have followed the documentation and my service is pretty much the same as described on the documentation:
#PostMapping("/someUrl")
public ResponseEntity<Void> uploadFile(#RequestPart(name="foo", required = false) Foo foo, #RequestPart("file") MultipartFile file) {
return new ResponseEntity<>(OK);
}
When I try to heat my service using Postman, I got the following error:
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
And on postman, I got the following return
The question is: Is it possible to call my service using Postman? If it is, what parameters am I missing?
Yes it is possible to call your service using Postman.
Instead of sending JSON as text, create a file (e.g. test.json) containing JSON data that you want to send:
{
"teste": "abc"
}
Then in form-data section change the type of your foo field from Text to File and choose JSON file that you've created.
And that's it. Click Send and your request should be processed without any error.
Note: Header should be left empty i.e. it should not contain any custom Content-Type value.
Looks like there is some issue with Postman while using multipart file with json data. https://github.com/postmanlabs/postman-app-support/issues/576
You can use curl to test your service like below.
curl -v -H "Content-Type:multipart/form-data" -F "foo={\"teste\":\"abc\"};type=application/json" -F "file=#amazon.png" http://localhost:8080/v1/files/someURL

Java http servlet get data inside -d parameter

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");

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.

Categories