简体   繁体   English

如何在 Flutter/Dart http 请求 object 上设置标题?

[英]How do I set headers on Flutter/Dart http Request object?

I need a way to set the headers of the dart http Request object to application/JSON.我需要一种方法将 dart http 请求 object 的标头设置为应用程序/JSON。

I want to build a Request object to send to my backend API.我想构建一个请求 object 发送到我的后端 API。 I set the body to my JSON object, but when it gets sent, it defaults the headers to text/html instead of application/json.我将正文设置为我的 JSON object,但是当它被发送时,它默认标题为 text/html 而不是 application/json。

I have tried using the built-in method我试过使用内置方法

http.post(url,dynamic body);

but unfortunately this method places the body in the parameters of the URL and I need it in the actual body of the request.但不幸的是,这种方法将正文放在 URL 的参数中,我需要它在请求的实际正文中。

So instead I built an http Request object, and manually set the URL and body but like I said, it sets the headers to text/html.因此,我构建了一个 http 请求 object,并手动设置 URL 和正文,但就像我说的那样,它将标题设置为 text/html。 I have read the docs for https://pub.dev/documentation/http/latest/http/Request-class.html , but unfortunately, I haven't found a way to set the headers.我已经阅读了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);
}


Much thanks for any possible solutions!非常感谢任何可能的解决方案!

Ps.附言。 this is my first ask on Stack Overflow so I apologize if I made any errors in posting.这是我在 Stack Overflow 上的第一个问题,所以如果我在发布时出现任何错误,我深表歉意。

Solved!解决了!

postRequest(uri) async {

    Uri url = Uri.tryParse("https://ptsv2.com/t/umt4a-1569012506/post");

    http.Request request = new http.Request("post", url);

    request.headers.clear();
    request.headers.addAll({"content-type":"application/json; charset=utf-8"});
    request.body = '{mediaItemID: 04b568fa, uri: https://www.google.com}';

    var letsGo = await request.send();

    print(letsGo.statusCode);
}

I was having some issues with the Request object default setting the encoding.我在 Request object 默认设置编码时遇到了一些问题。 By manually specifying utf-8, the server I am contacting accepts it.通过手动指定 utf-8,我正在联系的服务器接受它。

for the post or get any request you can Add Header like this - 

 var permAddUrl =  'your requested url';

 var bodyParameters =  {
      'Email': email,
      'MobileNo': mobileNumber,
    };

   await http.post(
     requesturl,
      headers: {  'Content-Type': 'application/x-www-form-urlencoded',
        "Authorization":"$token",
},

  body: bodyParameters,).then((response) {
      var data = json.encode(response.body);
      print(data);
      setState(() {
        if(response.statusCode == 200){
          //var statesList = data['data'];
          UtilAction.showSnackBar(context, " Details Submitted Successfully");

        }
      });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 POST 请求的响应标头发出新的 GET 请求? 颤振/飞镖/HTTP - How do I use the response header from a POST request to make a new GET request? Flutter/Dart/HTTP 如何在颤振/飞镖中发出 HTTP GET 请求 - How to make HTTP GET request in flutter/dart 如何对此 URL 执行 GET 请求? 颤振/飞镖 - How do I perform a GET request on this URL? Flutter/Dart 如何在 Flutter 中向 dio http 请求添加标头 - How to add headers to dio http request in Flutter 如何在HTTP请求上设置模块特定的自定义标头? - How can I set module specific custom headers on a HTTP request? Flutter Dart HTTP 标头不起作用。 发送带有“标题”的请求。 在响应授权错误时,{"CODE":401,"MESSAGE":"Unauthorized"} - Flutter Dart HTTP headers not working. Sending Request with "headers". On response authorization error, {"CODE":401,"MESSAGE":"Unauthorized"} 在Http get request dart flutter中发送一个json对象作为参数 - Send a json object as a parameter in the Http get request dart flutter 如何在http get请求中设置标头? - How to set headers in http get request? 如何使用 Reqwest 设置请求标头? - How do I set the request headers using Reqwest? Flutter - 如何在 Dart 中使用 HTTP 发送 POST 请求? - Flutter - How to send a POST request using HTTP in Dart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM