4

in DART lang, how to specify POST request Content-Type to be

multipart/form-data

My DART code is:

sendDatas(dynamic data) {
final req = new HttpRequest();
req.onReadyStateChange.listen((Event e) {
  if (req.readyState == HttpRequest.DONE &&
      (req.status == 200 || req.status == 0)) {
    window.alert("upload complete");
  }
});
req.open("POST", "/upload");
req.send(data);

}

I am doing POST with a file

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
bboy
  • 1,357
  • 2
  • 15
  • 31

2 Answers2

2

I think you should use HttpRequest.postFormData(url, data) here. Then you can use the following:

FormData data = new FormData(); // from dart:html

data.append(key, value);

HttpRequest.request('/upload', method: 'POST', sendData: data).then((HttpRequest r) {
  // ...
});

Regards, Robert

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Robert
  • 5,484
  • 1
  • 22
  • 35
  • any idea why I get this when making console `err` in Terminal: `no multipart boundary param in Content-Type` /// I'm still using my code – bboy Sep 09 '14 at 15:56
  • Can you show the complete code. My code should work. – Robert Sep 09 '14 at 16:52
  • when adding your code, I get something like this: `The argument type 'FormData (/Users/MyUserName/Downloads/dart/dart-sdk/lib/html/dartium/ html_dartium.dart)' cannot be assigned to the parameter type 'Map (/Users/MyUserName/Downloads/ dart/dart-sdk/lib/core/map.dart)'` – bboy Sep 10 '14 at 07:55
  • I changed my code. It was the wrong function that I used. – Robert Sep 10 '14 at 08:06
  • Concerning your error: I guess multipart/form-data requires the boundary value set: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 - e.g. 'Content-Type: multipart/form-data; boundary=AaB03x'. It is much better to use FormData as it does those things for you! – Robert Sep 10 '14 at 08:10
  • in your code, what are you using the `key` and `value` for? – bboy Sep 10 '14 at 08:14
  • It's the data for your form you want to submit. When you have a text input like `` then key is "name" and value is "Robert". – Robert Sep 10 '14 at 10:17
  • i'm having an `input type="file"`, doing it like you said, but still getting server err: `http: no such file` – bboy Sep 10 '14 at 10:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60957/discussion-between-bboy-and-robert). – bboy Sep 10 '14 at 10:35
0

On the server it is supported by the http package. This package can also be used in the browser but it seems the multipart_request.dart file can't be imported in the browser because it uses dart:io;

I suggest creating a feature request on http://dartbug.com/new.

You could also try to copy the code from the [http] package and remove the references to the io package and use browser API instead.

How it could be used on the server is shown here http://www.dartdocs.org/documentation/http/0.11.1+1/index.html#http/http.MultipartRequest

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567