Making GET, POST, PUT, PATCH, and DELETE requests
This article tells how to make HTTP requests using the http package by the Dart team. We will be using JSONPlaceholder as a target for our API examples below. Thanks to this post for the idea.
GET /posts
GET /posts/1
POST /posts
PUT /posts/1
PATCH /posts/1
DELETE /posts/1
I’ll demonstrate these with a simple app. You can find the code at the end of the article.
Setup
Add the http package dependency in pubspec.yaml.
dependencies:
http: ^0.13.3
Then add the following import at the top of the file where you’ll be making the HTTP requests:
import 'package:http/http.dart';
For the methods below, I’ll use the following URL prefix:
static const urlPrefix = 'https://jsonplaceholder.typicode.com';
GET requests
A GET request is used to get some resource from a server.
Future<void> makeGetRequest() async {
final url =…