ChrisPasa
ChrisPasa

Reputation: 113

receiving a json string in C# Web Api 2

I'm trying to do a patch with web api. I keep getting NULL for my json. Please Help

Here is my Json

[{"PartNumber":"AN33016UA-VB"}{"Category":"Chassis"}]

Here is my my class

public class wsCategory
{
    public string PartNumber { get; set; }

    public string Category { get; set; }
}

Here is my Api Controller

[HttpPatch]
[ActionName("IMDSCategory")]
public HttpResponseMessage IMDSCategory([FromBody]wsCategory jsonbody)
{
    var data = jsonbody.PartNumber;

    return new HttpResponseMessage(HttpStatusCode.Created);
}

Upvotes: 1

Views: 71

Answers (2)

Nkosi
Nkosi

Reputation: 246998

based on your method

[HttpPatch]
[ActionName("IMDSCategory")]
public HttpResponseMessage IMDSCategory([FromBody]wsCategory jsonbody){...}

Your JSON is invalid given the model you are trying to parse.

[{"PartNumber":"AN33016UA-VB"}{"Category":"Chassis"}]

should be

{"PartNumber":"AN33016UA-VB","Category":"Chassis"}

Upvotes: 1

Lathejockey81
Lathejockey81

Reputation: 1228

The JSON is inavalid.

[{"PartNumber":"blahblah","Category":"Chassis"}]

I believe the array container will be parsed out correctly, but I'm on a chromebook right now, so I can't check that. If it still fails, drop the [].

Upvotes: 1

Related Questions