I am attempting to use CURL to send a simple POST parameter (an int) to an ASP.NET web api controller POST method like so:
curl -d "id=1" --ntlm --user <user>:<pass> http://dev.test.local/api/test
Is this the correct way to attach data to the POST for Curl? I can contact the URL fine but it seems the parameter ‘id’ doesn’t get passed because i get the following error returned from the server:
"The parameters dictionary contains a null entry for parameter 'id' of non-nulla
ble type 'System.Int32' for method 'System.String Post(Int32)' in 'Test.Si
te.Controllers.TestController'. An optional parameter must be a reference type,
a nullable type, or be declared as an optional parameter."
My POST method in the OrderController is as follows:
// POST api/test
public string Post(int id)
{
return "Post successful";
}
Any help is much appreciated.
Personally I would use a simple DTO and call via JSON.
route:
controller & DTO:
Call with curl:
But
Just to follow on a little from tugberk’s answer and referencing another answer and here .
When you use the FromBody attribute you will also need to send the “Content-Type” as Content-Type: application/x-www-form-urlencoded. You will also need to change the call not have the “id=1” and instead use “=1” e.g.