Slinky
Slinky

Reputation: 5832

Handling posted JSON value in .NET

All,

I am working on a web service that listens for posts with a JSON payload. I have created a web app project in Visual Studio 2015, which provides a stubbed out API controller. When I send a test to the POST handler, the value received is null in the controller even though I am sending a JSON payload.

What am I doing wrong, here?

I suspect there is some type of binding I need, in my POST named "value", but since I am receiving a raw JSON payload, I cannot really affect the binding.

The payload that I am sending enter image description here

Returns null in "value" param value enter image description here

Upvotes: 0

Views: 203

Answers (2)

Nkosi
Nkosi

Reputation: 247048

Create a model to store the data being sent.

public class Payload {
    public string type { get; set;}
    public string id { get; set;}
    public long creates_at { get; set;}
    public long updated_at { get; set;}
    public string service_type { get; set;}
    public string[] topics { get; set;}
    public string url { get; set;}
    public bool active { get; set;}
    public string hub_secret { get; set;}
}

And update the controller action to accept the payload

public class ValuesController : ApiController {
    //POST api/values
    public void Post([FromBody] Payload value) {...}    
}

This assumes also that you are using the default convention-based route temaplate api/{controller}/{id}

Upvotes: 1

Nonik
Nonik

Reputation: 655

you need to put [System.Web.Http.HttpPost] attribute in your action/method

Upvotes: 0

Related Questions