简体   繁体   中英

How do I fetch data from an API using dart/flutter?

I want to send a request to an API in the below format

curl --request POST \
     --header 'Authorization: Token <token-id>' \
     --header 'Content-type: application/json' \
     --data '{"files": [{"name": "filename", "content": "xyz"}]}' \
     --url 'https://sampleurl.com'

How to do that? I've seen various questions/posts but none worked for me. PS, I am new to Flutter.

Flutter makes this quite simple with the http package . Here's a basic sample:

final String url = <your-url>; 

var body = {
  "param1Key": "param1Value", 
  "param2Key": "param2Value",
  };

var headers = {"Authorization": "Token <token-id>"};
var response = await http.post(url, headers: headers, body: body);

You need to use the http package in flutter. It provides anything you need to do networking and APIs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM