1

I'm trying to do a post request which fails whenever I add a second parameter to the function.

This for example works with the following function:

public class Mock
{
     public String MyFirstValue { get; set; }
     public String MySecondValue { get; set; }
}

[HttpPost]
public ResponseDataDTO TestPost(Mock test)
{
     var response = new ResponseDataDTO();
     return response;
}

With the following body request:

{ 
   "MyFirstValue": "testvalue", 
   "MySecondValue": "testvalue" }
}

However when I add another parameter to this function, it does not work anymore because I'm not quite sure of how to construct the json:

[HttpPost]
public ResponseDataDTO TestPost(Mock test, Mock test2)
{
      var response = new ResponseDataDTO();
      return response;
}

My best guess of a body:

{ 
   "test": {
               "MyFirstValue": "testvalue", 
               "MySecondValue": "testvalue"
   }
   "test2": {
               "MyFirstValue": "testvalue2", 
               "MySecondValue": "testvalue2"
   }
}

I haven't had these problems before becuase I usually just have one object parameter which takes it all.

Another strange thing that I can't get to work is a method with only an integer parameter:

[HttpPost]
public ResponseDataDTO TestPostInteger(int test)
{
      var response = new ResponseDataDTO();
      return response;
}

With body:

{ 
   "test" : 1
}

This gives me a 404, it wont even find the method. If I put the parameter in URL instead, it works.

1 Answer 1

2

The response body can only bind to one object.

Having said that, one solution would be to TestPost takes in an array of Mock. Like this:

 [HttpPost]
 public ResponseDataDTO TestPost(Mock[] tests)
 { ... }

Json:

[
   {
       "MyFirstValue": "testvalue", 
       "MySecondValue": "testvalue"
   },
   {
       "MyFirstValue": "testvalue2", 
       "MySecondValue": "testvalue2"
   }
]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I did not know that it could only bind to one object.

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.