How to extract parameters from curl request in jer

2019-03-06 11:16发布

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.

1条回答
叛逆
2楼-- · 2019-03-06 11:38

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.

查看更多
登录 后发表回答