Thursday, September 7, 2017

Tuple vs objects vs Request.Content with [FromBody] in an API hosted using MVC

posting using PostAsync as follows(input is a Tuple<string, string, string, string> )

var json = JsonConvert.SerializeObject(input);

var postUrl = $"{baseAddress}{urlEnd}";

var buffer = System.Text.Encoding.UTF8.GetBytes(json);

var byteContent = new ByteArrayContent(buffer);

byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

var response = await client.PostAsync($"{postUrl}", byteContent);


with an api endpoint like this:

public IHttpActionResult SaveText([FromBody] Tuple<string, string, string, string> text) 

it doesn't populate text with anything but nulls. I don't know why this is. If you replace it with an object you define('Myfavfourstringtuple) then it will populate Text without trouble. If you forgo any arguments and instead just look in the  Request.Content.ReadAsStringAsync().Result object that will also work. I think that [FromBody] is just not as smart as I'd like.

No comments:

Post a Comment