dart server: retrieve data from Request

326 Views Asked by At

I was trying to create a simple dart server, glad it's working, but...

   Future<Response> _echoHandler(Request request) async{
    final message = await request.readAsString();
    print(message);
    return Response.ok('$message\n');
  }

this is how that message looks like

----------------------------116375419757550841191749
Content-Disposition: form-data; name="message"

asdf
----------------------------116375419757550841191749--

Postman testingenter image description here

I want to extract this message.

3

There are 3 best solutions below

0
On

For me. I change form-data to raw. Then I can get data by using:

String jsonString = await request.cast<List<int>>().transform(utf8.decoder).join();
var data = json.decode(jsonString);

However, I want to use form-data too. I am converting another language server API to dart language. So, this must test in the same solution.

0
On

After I read this. I found shelf_multipart package can get data and files from the request with form-data.

You can follow my code.

2
On

you have to encode your data in the Json format then you can retrieve your data from the response