1

I have an MVC application with two micro services. In the first I call PostAsync() method to send my custom object

public class Logo { 
    public Guid Id { get; set; } 
    public IEnumerable<byte> Content { get; set; } 
}

to another service

public async Task PostLogo(Logo logo)
    {
        using (var client = new HttpClient())
        {
            await client.PostAsync(_url, new StringContent(JsonConvert.SerializeObject(logo), Encoding.UTF8, "application/json"));
        }
    }

In the second micro service I try to deserialize using

[HttpPost, Route("logo")]
    public Task<FileUploadResultModel> SaveAsync([FromBody]Logo model)
    {            
        return _fileService.SaveAsync(null);
    }

but it gets null instead of input object

enter image description here.

Can anyone explain how can I send/process custom object using Post request, please?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • update action to `Task SaveAsync([FromBody]OutlookLogo model)`. framework will deserialize for you and bind object model – Nkosi Aug 28 '17 at 14:19
  • Thank you for your answer. Unfortunately it is 'null' in the input string, before actual deserialization. I mean the 'model' value is null, but if I check 'JsonConvert.SerializeObject(logo)' it returns actual result with necessary parameters. So I think I'm doing something wrong while sending the request? – Egor Kovalev Aug 28 '17 at 14:23
  • It is the same but they are in different micro services. I've renamed the classes, but it is still null – Egor Kovalev Aug 28 '17 at 14:34
  • In that case it is possible that the byte array is too large so the model binder is unable to properly bind the model. – Nkosi Aug 28 '17 at 14:36
  • Also if you are able to get the image smaller you should base64 Url encode that image to a string and sent that as the content. – Nkosi Aug 28 '17 at 14:41
  • It looks like you are absolutely right. If I try to send only Id, it works great. Many thanks for your help. Do you have any idea how to send an image in such case? – Egor Kovalev Aug 28 '17 at 14:44
  • Check you web.config and see what is the max size of data that can be sent. You may need to increase it. The max is 2GB but I would advise against such a high value. – Nkosi Aug 28 '17 at 14:46

1 Answers1

1

Update action to follow preferred syntax structure.

[HttpPost, Route("logo")]
public async Task<IHttpActionResult> SaveAsync([FromBody]Logo model) {
    if(ModelState.IsValid) {
        FileUploadResultModel result = await _fileService.SaveAsync(model);
        return Ok(result);
    }
    return BadRequest();
}

Based on comments it is possible that the byte array is too large so the model binder is unable to properly bind the model. Check the web.config and see what is the max size of data that can be sent. You may need to increase it. The max is 2GB but I would advise against such a high value.

Check this answer: Maximum request length exceeded

If you are able to get the image smaller you should base64 Url encode that image to a string and sent that as the content.

public class Logo { 
    public Guid Id { get; set; } 
    public string Content { get; set; } 
}

And when you get the model convert the content back to a byte array

[HttpPost, Route("logo")]
public async Task<IHttpActionResult> SaveAsync([FromBody]Logo model) {
    if(ModelState.IsValid) {
        byte[] content = Convert.FromBase64String(model.Content);
        var id = model.Id;
        //...
        return Ok(result);
    }
    return BadRequest();
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472