6

PROBLEM

Trying to send a multipart/formdata post request with a file upload and model data in JSON. Not sure why there are not much working references, couldn't make work the ones which are there. Might be doing something wrong as well or making it more complicated too.

REFS TRIED

QUICK FIDDLE WITH MY TRIES

QUESTION

Somehow json content on my post request payload doesn't seem to contain Content-Type: application/json identified automatically.

Even setting the content-type as undefined/false for the POST didnt work. Need some suggestion on what would be the problem or how to fix this?

NETWORK LOG FOR FIDDLE UPLOAD

Request Headers

Accept:application/json, text/plain, */*
Content-Length:361
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary7NukqN6GtRwugBSJ

Request Payload

------WebKitFormBoundary7NukqN6GtRwugBSJ
Content-Disposition: form-data; name="file"; filename="sample.csv"
Content-Type: text/csv


------WebKitFormBoundary7NukqN6GtRwugBSJ
Content-Disposition: form-data; name="data"

{"name":"a good name","comments":"some comments"}
------WebKitFormBoundary7NukqN6GtRwugBSJ--
Community
  • 1
  • 1
raksja
  • 3,969
  • 5
  • 38
  • 44
  • 1
    this may help http://stackoverflow.com/questions/18203145/how-to-post-json-and-a-file-to-web-service-with-angular – Bilal BBB Sep 23 '15 at 10:00
  • 1
    thanks. its exactly the similar thing in fiddle posted, but its not sending that content-type for the json content. could you take a look at the fiddle and see whether i'm missing something? – raksja Sep 23 '15 at 16:57

2 Answers2

0

Faced the same issue.Try using blob and You can set the type in the blob as application/json. You can initialize the blob with your values and it worked for me.

-1

If you make a "multipart/form-data" request, then automatically, the "application/JSON" header will be left out. You cannot have both in the same request. The two post requests are mutually exclusive.

  • `Accept:` is a request header used for content negotiation by the server, informing the server what kind of response is supported by the client. It does not describe the content-type of the request payload. – Hank Jul 31 '18 at 08:22