2

Not able to post Array of string ids to Spring mvc controller using curl as it is throwing 400 status.

Controller Method:

@RequestMapping(value="/evidencesbyIds",method = RequestMethod.POST)  
@ResponseBody  
public Map<String, Object> getEvidenceByIds(@RequestBody String[] ids){
// body here
} 

CURL Commands:

curl  -H "Content-type: application/json"   -X POST -d '{"ids":["1","2"]}' http://localhost:8080/vt/evidencesbyIds 

curl  -H "Content-type: application/json"   -X POST -d "{"ids":["1","2"]}" http://localhost:8080/vt/evidencesbyIds  

curl  -H "Content-type: application/json"   -X POST -d "{"ids":[\"1\",\"2\"]}" http://localhost:8080/vt/evidencesbyIds  

curl  -H "Content-type: application/json"   -X POST -d "{\"ids\":[\"1\",\"2\"]}" http://localhost:8080/vt/evidencesbyIds  

I tried multiple times to execute curl commands but they are throwing status as 400 and exception as

"Can not deserialize instance of java.lang.String[] out of START_OBJECT token\n at"

1 Answer 1

3

Got the solution:

CURL command to post the String Array in request body :

curl -H "Content-type: application/json" -X POST -d "["1","2"]" http://localhost:8080/vt/evidencesbyIds

Note:
1. No Modification in controller method
2. The same can be used to if the controller accept List of String ids as request body.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.