【问题标题】:TRESTRequest: Add an Array as a parameter in a POST request. Delphi TokyoTRESTRequest:在 POST 请求中添加一个数组作为参数。德尔福东京
【发布时间】:2021-07-17 22:25:29
【问题描述】:

我有以下问题

我需要发出一个 POST 请求,它需要在其中一个参数中传递一组对象。

我的问题是:我该怎么做?

为了添加一个简单的参数,我以 TRESTRequest.AddParameter('api_token', xxxxxx ) 为例,它工作得很好

如果 AddParameter 不允许我以这种方式使用它,我如何将数组传递给请求?

【问题讨论】:

  • 请展示您想要获取的 JSON 示例。并显示您目前拥有的代码。
  • 参数的值是一个字符串。 HTTP 没有“参数中的数组”的概念。
  • @Olivier OP 用 JSON 标记了问题,所以我假设参数是 JSON 格式的字符串,它必须包含一个对象数组。
  • 但在我看来,实际的问题不在于请求应该如何。这里缺少的信息是,REST 服务器如何要求请求传递这个对象数组。首先在 Postman 工具中测试,请将结果放在这里,以帮助我们为您提供准确的答案。

标签: arrays json rest delphi


【解决方案1】:

我最近追了几天。我终于在 Windows 上安装了 Fiddler,看看 TRESTRequest 对我的参数做了什么。之后很容易在How to pass an array within a query string 上找到基本线索——正确的语法取决于服务器端的实现。

对于 Stripe API,它可以添加后缀“[]”并单独添加每个数组元素。

    // assuming these variable types exist already
    rr: TRESTRequest;
    i: Integer;
    Stripe_Payment_Method_Types: array of string;


    // here is the loop to pass the array of strings
    for i := 0 to High(Stripe_Payment_Method_Types) do
    begin
      // the [] is required to cause Stripe to see it as an array of strings
      rr.Params.AddItem('payment_method_types[]',  // <!-- trailing []
            Stripe_Payment_Method_Types[i]);
    end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    相关资源
    最近更新 更多