【问题标题】:How to send https post request in "flutter / dart" with an if statement如何使用 if 语句在“flutter / dart”中发送 https 发布请求
【发布时间】:2020-01-03 00:02:51
【问题描述】:

我正在编写一个跨平台二维码扫描支付应用程序。我想让我的扫描值发送到需要 json 正文的第三方服务器 api。如何创建发送我的 api post 请求的 if 语句?

我已经尝试结合 qr 值作为键( if qrValue.isNotEmpty == true )实现 if/then ,但从这一点上我完全迷失了。对于这个问题,我似乎找不到我理解的信息来源。

尤其是使用 api 后调用

我需要发送到的网址是:https://api.ext.payconiq.com/v3/payments

邮递员告诉我正文和标题是正确的///

///

var qrValue = ""; 
  var amount = "1";
  var callbackurl = "api.vendingshop.nl:1880/ui/rest/payments";
  var description = "Test Payment for opening lock";
  var reference = "1";


 if ( qrValue.isNotEmpty == true){

  /* Here needs to come the value for the api input to send and receive the api calls to payconiq*/

  }


  /*



/* Here comes the "Headers"
content type: application / json
cache control: no cache
Authorization: /*apicode*/

"body"
{
         "amount" : "1",
         "callbackurl": "api.vendingshop.nl:1880/ui/rest/payments",
         "description": "Test payment for opening lock",
         "reference": "1"}

 ;
 }
}*/

////

【问题讨论】:

  • 你到底想发布什么?请举个例子。
  • 我希望这更具可读性: If qrValue == qrValue.IsNotEmpty then http.post json body & json headers

标签: api flutter https dart


【解决方案1】:

Dart 非常适合快速高效地实现此类操作,这一切都归功于其强大的 http 库。

您所要做的就是在您的 dart 文件中导入 http 库并从异步函数执行 post 操作。

import 'dart:http' as http;
[...]
void sendInfo({String amount,String description,String reference,String callbackurl})
 async{
 http.post(
 callbackurl,
 body: json.encode(
   {
     "amount" : amount,
     "description": description,
     "reference": reference
   }
 )     
 ).then((response){
print("Response Code : ", response.statusCode);
print("Response Body : ", response.body);
// Perform the required operation(s)
 });
}

【讨论】:

  • 谢谢!这已经有很大帮助了,您是否对如何添加多个标题有任何意见?例如 Json/encode,没有缓存和授权
  • 如果您认为答案对您有所帮助,请将其标记为正确,以便其他人可以从中受益。
  • 您可以参考此线程以获取有关发送完整 HTTP 请求的更详细说明。 stackoverflow.com/questions/49797558/…
猜你喜欢
  • 2021-01-26
  • 2019-12-04
  • 2015-02-27
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多