Search code examples
javascriptxmlhttprequestmultipartcasablancacpprest-sdk

Sending multipart/related request via javascript and XmlHttpRequest


I'm trying to send multipart/related request to my own server based on cpprestsdk:

POST /test HTTP/1.1
Content-Type: multipart/related; boundary=-
---
Content-Type: application/json

{
   "testField": "testValue"
}
---
Content-Type: image/jpeg
Content-Length: 131745
Content-Transfer-Encoding: binary

[DATA (131745 bytes)]
---

The second part of request contains jpeg image in binary format and server successfully reads it and creates an image from that bytes. But then a see that request in Wireshark, it show about 192000 bytes of the image data, not 131745 as I expect. I assume that all of the request data converted to utf-8 by javascript's XmlHttpRequest because charset for multipart/related was not specified. Is there any way to avoid such overhead and send only original bytes?


Solution

  • The problem was that I passed request to the send method of XmlHttpRequest as a string value, not as an ArrayBuffer