2

I have a client which is sending a json to my asp.net mvc application. Where can I receive the json body?

sending:

        var client = new RestClient(uri);
        client.Authenticator = new NtlmAuthenticator();

        RestRequest requestCom =
            new RestRequest("", method);

        //add headers
        requestCom.AddHeader("Accept", "application/json");

        if (body != null)
        {
            requestCom.AddJsonBody(body);
        }

        IRestResponse response = client.Execute(requestCom);

controller:

    public string Index([FromBody]object body)
    {
        return body.ToString();
    }

The url is my controller in the mvc application. So how can i receive the body?

Gian-Luca
  • 85
  • 8

1 Answers1

3

Lets say you have a model like this

public class MyModel {
    public string AProperty { get; set; }
}

And send to the server

var client = new RestClient(uri);
client.Authenticator = new NtlmAuthenticator();

var requestCom = new RestRequest("", method);
//add headers
requestCom.AddHeader("Accept", "application/json");

var body = new MyModel {
    AProperty = "Hello World!!!"
}

if (body != null) {
    requestCom.AddJsonBody(body);
}

IRestResponse response = client.Execute(requestCom);

The Controller action would have to expect the model in the body

public string Index([FromBody]MyModel body) {
    return body.ToString();
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472