Flutter: Http Client with Headers(Zomato api)

 

HTTP requests in Flutter

GET requests

_makeGetRequest() async {  // make GET request
String url = 'https://jsonplaceholder.typicode.com/posts';
Response response = await get(url);
// sample info available in response
int statusCode = response.statusCode;
Map<String, String> headers = response.headers;
String contentType = headers['content-type'];
String json = response.body;
// TODO convert json to object...}

Now replace /posts with /posts/1 in the url. Using /posts returns an array of JSON objects while /posts/1 returns a single JSON object, where 1 is the ID of the post you want to get.


POST request

A POST request is used to create a new resource.

_makePostRequest() async {  // set up POST request arguments
String url = 'https://jsonplaceholder.typicode.com/posts';
Map<String, String> headers = {"Content-type": "application/json"};
String json = '{"title": "Hello", "body": "body text", "userId": 1}';
// make POST request
Response response = await post(url, headers: headers, body: json);
// check the status code for the result
int statusCode = response.statusCode;
// this API passes back the id of the new item added to the body
String body = response.body;
// {
// "title": "Hello",
// "body": "body text",
// "userId": 1,
// "id": 101
// }
}

PUT request

A PUT request is meant to replace a resource or create it if it doesn’t exist.

_makePutRequest() async {  // set up PUT request arguments
String url = 'https://jsonplaceholder.typicode.com/posts/1';
Map<String, String> headers = {"Content-type": "application/json"};
String json = '{"title": "Hello", "body": "body text", "userId": 1}';
// make PUT request
Response response = await put(url, headers: headers, body: json);
// check the status code for the result
int statusCode = response.statusCode;
// this API passes back the updated item with the id added
String body = response.body;
// {
// "title": "Hello",
// "body": "body text",
// "userId": 1,
// "id": 1
// }
}

PATCH request

A PATCH request is meant to modify an existing resource.

_makePatchRequest() async {  // set up PATCH request arguments
String url = 'https://jsonplaceholder.typicode.com/posts/1';
Map<String, String> headers = {"Content-type": "application/json"};
String json = '{"title": "Hello"}';
// make PATCH request
Response response = await patch(url, headers: headers, body: json);
// check the status code for the result
int statusCode = response.statusCode;
// only the title is updated
String body = response.body;
// {
// "userId": 1,
// "id": 1
// "title": "Hello",
// "body": "quia et suscipit\nsuscipit recusandae... (old body text not changed)",
// }
}

Notice that the JSON string that is passed in only includes the title, not the other parts like in the PUT example.

DELETE request

A DELETE request is of course for deleting resources.

_makeDeleteRequest() async {  // post 1
String url = 'https://jsonplaceholder.typicode.com/posts/1';
// make DELETE request
Response response = await delete(url);
// check the status code for the result
int statusCode = response.statusCode;
}

Authentication

Although the demo site we used above did not require it, in real life you need to include authentication headers, especially when doing things other than GET requests. You can add add the headers to any request, though usually the basic credentials (username and password) would be added to an initial POST request (to sign in) and then you would use the returned token for subsequent requests.

Basic Authentication

// import 'dart:convert'
// import 'dart:io';
final username = 'username';
final password = 'password';
final credentials = '$username:$password';
final stringToBase64 = utf8.fuse(base64);
final encodedCredentials = stringToBase64.encode(credentials);
Map<String, String> headers = {
HttpHeaders.contentTypeHeader: "application/json", // or whatever
HttpHeaders.authorizationHeader: "Basic $encodedCredentials",
};

In an OAuth setup you might put the client ID and secret in the auth header and the username and password in the body.

Note that I used the HttpHeaders constants here, which required the dart:io import. Feel free to use the strings "content-type" and "authorization" instead.

 

Comments

Popular posts from this blog

Flutter: Exploring more widgets

Flutter: Everything is a widget

Food Delivery App 5 - Firebase Storage for Profile Picture