我需要一种将 dart http 请求对象的标头设置为 application/JSON 的方法。
我想构建一个请求对象以发送到我的后端 API。我将正文设置为我的 JSON 对象,但是当它被发送时,它默认标题为 text/html 而不是 application/json。
我尝试使用内置方法
http.post(url,dynamic body);
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这种方法将主体放在 URL 的参数中,而我需要在请求的实际主体中使用它。
因此,我构建了一个 http 请求对象,并手动设置了 URL 和正文,但就像我说的那样,它将标题设置为 text/html。我已经阅读了https://pub.dev/documentation/http/latest/http/Request-class.html的文档,但不幸的是,我还没有找到设置标题的方法。
postRequest(uri) async {
Uri url = Uri.tryParse("https://ptsv2.com/t/umt4a-1569012506/post");
http.Request request = new http.Request("post", url);
request.body = '{mediaItemID: 04b568fa, uri: https://www.google.com}';
var letsGo = await request.send();
print(letsGo.statusCode);
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何可能的解决方案!
附言。这是我对 Stack Overflow 的第一次提问,所以如果我在发布时犯了任何错误,我深表歉意。