1

I try to send serialized model in json and a file to a web api on the same request, but I am not able to achieve it, my code below

Web aplication

        HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;
        request.Method = Verbo;
        request.ContentType = "application/json; charset=utf-8";

        foreach (var item in Body)
        {
            request.Headers.Add(item.Key, item.Value);
        }


        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = JsonConvert.SerializeObject(obj, Formatting.Indented);

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }


        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        StreamReader reader = new StreamReader(response.GetResponseStream());
        respuesta = reader.ReadToEnd();

Web Api

       [HttpPost]
       [Route("Insert")]
       public async Task<OperationResponse> Insert([FromForm]Brand Model)
        {
        var res = new OperationResponse
        {
            Ok = false,
            Message = string.Empty
        };


        return res;
        }

The binding Model is Null

My Json

     {
     "Name": "Jon Doe",
     "TelephoneNumber":"9999999",
     "Email": "test@hotmail.com",
     "Active": false,
     "File": {
     "PathFile": {
         "ContentDisposition": "form-data; name=\"Brand.FileName\"; filename=\"test.p12\"",
         "ContentType": "application/x-pkcs12",
         "Headers": {
             "Content-Disposition": [
                 "form-data; name=\"Brand.FileName\"; filename=\"test.p12\""
              ],
         "Content-Type": [
         "application/x-pkcs12"
            ]
           },
      "Length": 6712,
      "Name": "Brand.FileName",
      "FileName": "test.p12"
      }
     }

am I missing a step?

Thanks a lot.

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
Ciel
  • 11
  • 1
  • SUGGESTIONS: 1) Try sending a request to the WebAPI with [Postman](https://www.getpostman.com/downloads/).. Q: Did the request arrive in `Insert()`? 2) Step through the code in the debugger. Q: Is `Url` correct? Did the connection succeed? What HTTP response code did you get? Please update your post with this information. – FoggyDay Jan 04 '20 at 17:44

2 Answers2

0

Postman enters the method insert, but still does the bind in null, the code estaus is 200.

Ciel
  • 11
  • 1
0

Since your content type is application/json; charset=utf-8, you need to use [FromBody] instead of [FromForm].

[HttpPost]
[Route("Insert")]
public async Task<OperationResponse> Insert([FromBody]Brand Model)
Ryan
  • 19,118
  • 10
  • 37
  • 53
  • Hi Xing Zou, Try With FromBody but you still don't do the json bind. The issue that is if I remove the PathFile field bind the model correctly. I think I have to make the post in a different way to send a model with an attachment. – Ciel Jan 06 '20 at 05:03
  • @Clet How will you call the api,in the controller or from the view when you select a file and submit form?Do you mean the `Brand` model has property `public IFormFile PathFile {get;set;}`?Is this what you find:https://stackoverflow.com/questions/41367602/upload-files-and-json-in-asp-net-core-web-api – Ryan Jan 06 '20 at 05:22