[英]How to make an http DELETE request with JSON body in Flutter?
In flutter project, I want to perform a DELETE request with JSON body.在颤振项目中,我想用 JSON 正文执行DELETE请求。 But whenever I am trying to use http.delete method it is showing me - The named parameter 'body' isn't defined.但是每当我尝试使用 http.delete 方法时,它都会向我显示 - 未定义命名参数“body”。
Here's the example of my API delete request-这是我的 API 删除请求的示例 -
url: 'BASE_URL'+notes/delete; url: 'BASE_URL'+注释/删除;
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.因此,我需要使用上面提到的以下正文和标头发出有关 DELETE 请求,并且从 JSON 响应中我需要将状态值保存在字符串中。 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.请告知以上述方式发出 DELETE 请求并获得响应的整个过程。
You can use Request from http package:您可以使用 http 包中的请求:
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();
}
Short answer:简短的回答:
A http delete request does not support a body. http 删除请求不支持正文。 Normally you just use an identifier in your url to identify the object to delete like this :通常,您只需在 url 中使用标识符来标识要删除的对象,如下所示:
DELETE api.com/entity/1234删除 api.com/entity/1234
1234 is the ID of the object. 1234 是对象的 ID。
Long answer :长答案:
In your case you want to make a POST request to that url.在您的情况下,您想向该网址发出 POST 请求。
There is a dart package that provides some helper classes for http requests.有一个 dart 包为 http 请求提供了一些帮助类。
Github : https://github.com/Ephenodrom/Dart-Basic-Utils Install it with: Github: https : //github.com/Ephenodrom/Dart-Basic-Utils安装:
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.这些都是来自 HttpUtils 类的方法。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.