Search code examples
c#asp.net-web-apienterprise-library

How request body parameter access by Web API using Content-Type: multipart/form-data?


When I am passing parameter from Request body in JSON format then not coming hit to web api. My code:

    public async Task<HttpResponseMessage> Upload([FromBody]AddPhotoBizPrm addPhotoBizPrm)
    {
        json_obj.RESPONSE = new { };
        string images = "";
        if (Request.Content.IsMimeMultipartContent())
        {

            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);
            foreach (var file in provider.Contents)
            {
                var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
                var buffer = file.ReadAsByteArrayAsync();
                images += filename;
                //Do whatever you want with filename and its binary data.
            }
        }
        json_obj.RESPONSE = images;
        json_obj.MESSAGE = "";
        json_obj.STATUS_CODE = 701;
        return new HttpResponseMessage()
        {
            Content = new StringContent("ok")
        };
    }

and I am passing parameter from request body:

{
deviceid:"",
rate:2,
review:"",
skuid:123,
userId:875904
}

Solution

  • Solved this problem. I was using content-type=multipart/form-data so we can not pass json parameter to request body. So we will pass paramter in data form as like deviceid=""&rate=2&review=""&skuid=123&userId=875904