How to make an http DELETE request with JSON body in Flutter?
Solution 1
You can use Request from http package:
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<String> deleteWithBodyExample() async {
final baseUrl = "baseUrl";
final url = Uri.parse(baseUrl + "notes/delete");
final request = http.Request("DELETE", url);
request.headers.addAll(<String, String>{
"Accept": "application/json",
"token": "my token",
"jwt" : "my jwt"
});
request.body = jsonEncode({"id": 4});
final response = await request.send();
if (response.statusCode != 200)
return Future.error("error: status code ${response.statusCode}");
return await response.stream.bytesToString();
}
Solution 2
Short answer:
A http delete request does not support a body. Normally you just use an identifier in your url to identify the object to delete like this :
DELETE api.com/entity/1234
1234 is the ID of the object.
Long answer :
In your case you want to make a POST request to that url.
There is a dart package that provides some helper classes for http requests.
Github : https://github.com/Ephenodrom/Dart-Basic-Utils Install it with:
dependencies:
basic_utils: ^1.4.0
Usage
Map<String, String> headers = {
"Accept": "application/json",
"token": "my-token",
"jwt" : "my-jwt"
};
String URL = BASE_URL+"notes/delete" ;
String body = "{\"id\":\"4\"}"
Map<String, dynamic> dataAsJson = await HttpUtils.postForJson(url,body,
headers: headers);
Additional information :
These are all methods from the HttpUtils class.
Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Map<String, dynamic> getQueryParameterFromUrl(String url);
String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);
S. M. Asif
Updated on December 12, 2022Comments
-
S. M. Asif over 1 year
In flutter project, I want to perform a DELETE request with JSON body. But whenever I am trying to use http.delete method it is showing me - The named parameter 'body' isn't defined.
Here's the example of my API delete request-
url: 'BASE_URL'+notes/delete;
Header:
Content-Type : 'application/json', token: 'my token', jwt: ' my jwt'
Body:
{ "id":"4" }
Response:
status: "Deleted"
So, I need to make the regarding DELETE request with the following body and headers mentioned above and from the JSON response I need to save the value of status in a String. Here, I need a help with the code to make this delete request.
Please inform the entire procedure to make the DELETE request and getting response in the above mentioned way.