Ravinder
Ravinder

Reputation: 41

How to call an endPoint in curl that is having two request body parameters

I have defined a method like this

@RequestMapping(value="/multiRquestBody",method=RequestMethod.POST)
    public String multiRquestBodyMethod(@RequestBody String[] body1,@RequestBody String[] body2){
        System.out.println("body1 : "+body1);
        System.out.println("body 2 : "+body2);
        return Arrays.toString(body1)+"------"+Arrays.toString(body2);
    }

I used curl command like this to call that method

curl -X POST  "http://localhost:7979/choudhury-rest/rest/book/multiRquestBody"     -d '["test","test","test"],["testing","testing string array"]' -H "Content-Type: application/json"

Then I got an error like this

The request sent by the client was syntactically incorrect.

I have tried another way like

curl -X POST  "http://localhost:7979/choudhury-rest/rest/book/multiRquestBody"     -d '["test","test","test"]&["testing","testing string array"]' -H "Content-Type: application/json"

But still, the same issue is coming how can I solve it

Upvotes: 1

Views: 462

Answers (1)

xProgramery
xProgramery

Reputation: 517

@RequestBody should ideally be used only once in the method and hold the entire body of the request. In your case, you can create an object that holds the two string arrays, so something like this:

@RequestMapping(value="/multiRquestBody",method=RequestMethod.POST)
public String multiRquestBodyMethod(@RequestBody StringArraysBody body){
    System.out.println("body1 : "+body.getBody1());
    System.out.println("body 2 : "+body.getBody2());
    return Arrays.toString(body.getBody1())+"------"+Arrays.toString(body.getBody2());
}

public class StringArraysBody {
   String[] body1;
   String[] body2;

   public String[] getBody1() {
     return body1;
   }

   public String[] getBody2() {
     return body2;
   }
}

Upvotes: 1

Related Questions