Asp.net-core – Calling web api PUT FromBody “value” always null

asp.net-coreasp.net-web-api

I am trying to call the ValuesController WebApi controller (that gets created by default) "PUT" method using cURL. No matter what I do, send value="abc", "abc" or "=abc" as I saw suggested in other questions for the POST method, to no avail.

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

I also tried changing the Content-Type to application/json and application/x-www-form-urlencoded, nothing seems to work.

curl -H -d "=TEST" -X PUT  http://localhost:30960/api/values/5

Is this a bug in ASP.NET 5 Web Api or is there a different format I need to use when calling?

Best Answer

In order to make it work you need to pass it as a simple string:

"any value here"

There is even a test for this behaviour

Remember to add an application/json Content-Type header when issuing a request.

I've just tested it for both PUT and POST requests.

Using curl it's:

curl -H "Content-Type: application/json" -d "'Test'" -X PUT http://localhost:[port]/api/values/5
Related Topic