shakky
shakky

Reputation: 474

How to pass header and body in the HTTP call - Flutter

I'm relatively new to flutter. I'm trying to make an API call using the flutter HTTP package (along with header and body)

For example, how could I make the same call in Flutter HTTP?

curl --location --request POST 'xyz.com' \
--header 'Referer: {{your app package name or website url}}' \
--header 'API-KEY: {{api-key}}' \
--data-urlencode 'vehicleId=MHxxxxxxxx' // how to pass this in http body

This is what I could do so far, am I doing it correctly so far? How can I pass body content?

Map<String, String> headers = {"Referer": "abcd.com", "API-KEY": "abcd12345"};
 var url = Uri.parse('xyz.com');
 var response = await post(url, headers: headers);

Upvotes: 2

Views: 3897

Answers (1)

Ananda Pramono
Ananda Pramono

Reputation: 1009

to create a header for HTTP call, you can do it this way. Hope its help you.

  Map<String, String> _header = <String, String>{
      "Referer": "abcd.com", 
      "API-KEY": "abcd12345"
  };

  final response = await http.post(
      Uri.parse(url),
      headers: _header,
      body: data
  );

Upvotes: 2

Related Questions