1

I have been trying to figure this our for the last hour, but I can't see what's wrong with it

the post is from a xamarin app that I'm writing, using restsharp portable client

POST http://192.168.30.103:8080/api/Order HTTP/1.1
Authorization: Basic xxxx
Content-Length: 51
Content-Type: application/json; charset=utf-8
Host: 192.168.30.103:8080
Connection: Keep-Alive
Pragma: no-cache

{"CheckinCount":1,"EventId":16630,"OrderNo":470292}

It hits the server Post API correctly, but the parameter is always null

 public IHttpActionResult Post([FromBody]string source) {
        dynamic data = JsonConvert.DeserializeObject(source);
        int checkInCount = data.CheckinCount;
        int eventId = data.EventId;
        int orderNo = data.OrderNo;
        return Ok();
    }

I can't figure out why...

3
  • Why do you do the deserialization manually and not leave it to web api? Commented Dec 2, 2016 at 7:39
  • {"CheckinCount":1,"EventId":16630,"OrderNo":470292} isn't a string. It's an object instead. Commented Dec 2, 2016 at 7:44
  • I didn't want to add another DTO for a last minute API addon, but I'll give it a whirl Commented Dec 5, 2016 at 0:31

3 Answers 3

3

Do you always send the same parameters? If so, could you create a static object instead of using a dynamic one? Something like an EventRequest that you pass in instead?

public class EventRequest
{
    public int CheckinCount { get; set; }
    public int EventId { get; set; }
    public int OrderNo { get; set; }
}

Your Post action then becomes:

public IHttpActionResult Post(EventRequest request) {
    // do something with request
    return Ok();
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks that worked, however i'd still be interested if I wanted to read raw json from body, how would I do it ?
Just for my own note, if you forget public on the properties, it won't give you an error, just an empty object with null values for properties. I promise to never ever copy and paste code again!
1

For me, it was the value of that is being sent by checkbox. Checkbox was sending true but for JSON, value must be between quotations i.e. "true".

Comments

0

related entry from this link

in short, when sending simple values which are to be picked up using [frombody] attribute on the server side, you have to prefix the value with = e.g. =testValue

but its probably better just to use JSON Objects and object bindings on the API, even for simple values

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.