Wrong question asked, see my update below
I need to integrate my AngularJS Project with an existing RESTful API. These API consume POST request which upload a file
, and also submit the form data in a request. Unfortunately, one of the form input requires to be in Content-Type: Application/json
.
After search on the web, I could only POST
with Content-Type: multipart/form-data
in which each of the parts does not have a specific MIME
.
How can I compose my multipart/form-data
with a different MIME
for each parts in Javascript?
POST /api/v1/inventory
Host: localhost:8000
Origin: http://localhost:9000
Content-Type: multipart/form-data; boundary=------border
------border
Content-Disposition: form-data; name="owner"
john doe
------border
Content-Disposition: form-data; name="image"; filename="mybook.png"
Content-Type: image/png
------border
Content-Disposition: form-data; name="items"
Content-Type: application/json
{"name": "Book", "quantity": "12"}
------border--
Relevant References:
- https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects
- REST - HTTP Post Multipart with JSON
- http://code.activestate.com/recipes/578846-composing-a-postable-http-request-with-multipartfo/
- application/x-www-form-urlencoded or multipart/form-data?
- https://mcmap.net/q/210368/-rest-http-post-multipart-with-json
Update
Apologize for asking a wrong question. The original problem is that, I can see the server calling the logic something like,
func POST(req):
owner = req.owner // This is string
image = req.image // This is file object
itemQuantity = req.items.quantity // Items is an object with attribute quantity
itemName = req.items.name // Items is an object with attribute name
I have also managed to figure out how to submit such a post request. I will post my answer below.
Once again sorry for asking a wrong question.