I'm building a Web App based on Dart (with polymer framework but that's not relevant for the question). My app is communicating with a HTTP RESTful server.
Problem is that i'm trying to send an HTTP Post request in multipart, but I couldn't find a way to set the 'Content-Type' for each part of the body (and not for the main HTTP header, which is rightly set by dart to 'multipart/form-data')
Here is a simple repro of my http request :
import 'dart:html';
import 'dart:convert';
main(){
print("je suis dans dart");
Map jsontosend = {
'business-context-id': "contexte1",
'metadatas': [
{"metadata-id" : "nom", "value" : "doe"},
{"metadata-id" : "prenom", "value" : "john"}],
};
FormData form = new FormData();
form.append("context", JSON.encode(jsontosend));
HttpRequest.request("www.google.com", method: "POST", sendData: form);
}
Here is my request payload :
Remote Address:127.0.0.1:63342
Request URL:http://localhost:63342/http_test/web/www.google.com
Request Method:POST
Status Code:405 Method Not Allowed
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:263
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarym9PvtGzZDKOKiaH1
Host:localhost:63342
Origin:http://localhost:63342
Referer:http://localhost:63342/http_test/web/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.0 (Dart) Safari/537.36
Request Payload
------WebKitFormBoundarym9PvtGzZDKOKiaH1
Content-Disposition: form-data; name="context"
{"business-context-id":"contexte1","metadatas":[{"metadata-id":"nom","value":"doe"},{"metadata-id":"prenom","value":"john"}]}
------WebKitFormBoundarym9PvtGzZDKOKiaH1--
The server i'm trying to call can't parse the 'context' part as there is no content-type provided.
I also tried to 'convert' my json as a blob object :
import 'dart:html';
import 'dart:convert';
main(){
print("je suis dans dart");
Map jsontosend = {
'business-context-id': "contexte1",
'metadatas': [
{"metadata-id" : "nom", "value" : "doe"},
{"metadata-id" : "prenom", "value" : "john"}],
};
FormData form = new FormData();
Blob blob = new Blob([JSON.encode(jsontosend)], 'application/json', 'native');
form.appendBlob("context", blob);
HttpRequest.request("www.google.com", method: "POST", sendData: form);
}
And the associated payload :
Remote Address:127.0.0.1:63342
Request URL:http://localhost:63342/http_test/web/www.google.com
Request Method:POST
Status Code:405 Method Not Allowed
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:312
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryjHxHOCKDpsp22rzQ
Host:localhost:63342
Origin:http://localhost:63342
Referer:http://localhost:63342/http_test/web/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.0 (Dart) Safari/537.36
Request Payload
------WebKitFormBoundaryjHxHOCKDpsp22rzQ
Content-Disposition: form-data; name="context"; filename="blob"
Content-Type: application/json
------WebKitFormBoundaryjHxHOCKDpsp22rzQ--
This time, the Content-Type of the context part is set, but my json is 'wrapped' as a blob (converted to Base64 by dart framework?) and can't be parsed by the server.
As soon as I use the Blob type, the filename attribute is set, and the server also rejects the request.
Any idea ?
Thanks in advance !