I am using dart:io
to create the server. I send the request from the Postman
with form-data
. I need to use form-data
because my old API from another language uses it and the app uses it too.
At the moment. I am trying to get the data and files with this code:
Future main(List<String> arguments) async {
HttpServer server = await HttpServer.bind('localhost', 8085);
server.listen((HttpRequest request) async {
String jsonString = await request.cast<List<int>>().transform(utf8.decoder).join();
print("jsonString:\n$jsonString");
await request.response.close();
});
}
When I send the data and a file from the Postman
with this below.
I will get the error below.
Unhandled exception:
FormatException: Unexpected extension byte (at offset 435)
If I don't send the file as image 1
, I got this.
jsonString:
----------------------------166099235909119466948633
Content-Disposition: form-data; name="key 1"
Content-Type: application/json
value 1
----------------------------166099235909119466948633
Content-Disposition: form-data; name="key 2"
value 2
----------------------------166099235909119466948633--
I can't convert the above results to variables. I don't know how to do that. Has anyone an example for doing this or suggest any package to me? This is my first time creating a dart server.
I follow this. You can get the data and files from the request by using shelf_multipart (Other packages may be used in conjunction with this one and find more methods on GitHub).
If you want to see results quickly that it can be done. Follow this below.
I am using 3 packages including the shelf, shelf_router, and shelf_multipart packages.
You need to add these packages to your
pubspec.yaml
. (You can copy and paste these into yourpubspec.yaml
.)Then copy my code and past it to your
main.dart
:After this, you can start your server in your terminal. For me I am using:
Finally, open the Postman and paste
http://localhost:8085/example
to the URL field, select thePOST
method, andform-data
. You can add the data into theKEY
andVALUE
fields. Then press send.This is my example in the Postman:
This solution work with
http.MultipartRequest()
from the Flutter app.