加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何将查询参数添加到Dart http请求?

发布时间:2020-12-14 14:52:52 所属栏目:百科 来源:网络整理
导读:如何正确地将查询参数添加到Dart http get请求?在尝试将’?param1 = one param2 = two’附加到我的网址时,我无法正确回应我的请求,但它在Postman中正常工作.这是我的代码的要点: final String url = "https://www.myurl.com/api/v1/test/"; String workin
如何正确地将查询参数添加到Dart http get请求?在尝试将’?param1 = one& param2 = two’附加到我的网址时,我无法正确回应我的请求,但它在Postman中正常工作.这是我的代码的要点:

final String url = "https://www.myurl.com/api/v1/test/";
    String workingStringInPostman = "https://www.myurl.com/api/v1/test/123/?param1=one&param2=two";

    Map<String,String> qParams = {
     'param1': 'one','param2': 'two',};


   var res = await http
      .get(Uri.encodeFull("$url${widget.pk}/"),headers: {HttpHeaders.authorizationHeader: "Token $token",HttpHeaders.contentTypeHeader: "application/json"},);

${widget.pk}只是一个传递的整数值(请参阅workingStringInPostman变量中的值123.

如果需要Uri参数,qParams可以用于connivence.

一个代码示例将受到欢迎.

解决方法

您将要构建一个Uri并将其用于请求.就像是

var queryParameters = {
  'param1': 'one',};
var uri =
    Uri.https('www.myurl.com','/api/v1/test/${widget.pk}',queryParameters);
var response = await http.get(uri,headers: {
  HttpHeaders.authorizationHeader: 'Token $token',HttpHeaders.contentTypeHeader: 'application/json',});

见https://api.dartlang.org/stable/2.0.0/dart-core/Uri/Uri.https.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读