【问题标题】:How to pass array to Postman as form-data如何将数组作为表单数据传递给 Postman
【发布时间】:2021-06-09 14:56:58
【问题描述】:

我想在 Postman 中测试以下 URL。但是,我想分解数组以便于测试,例如作为表单数据(或其他)。如何在 Postman 中设置这个数组?

完整网址

/inventory-results?query={"query":{"model":"xyz","condition":"new","options":{},"arrangeby":"Price","order":"asc","market":"CA","language":"en","super_region":"north america"}}

更新:

How would I build this URL in Swift 5.x using URLComponents?

        var urlComponents = URLComponents()
        urlComponents.scheme = "https"
        urlComponents.host   = "www.yoururl.com"
        urlComponents.path   = "/api/v1/inventory-results"
        
        
        let query = [
                     URLQueryItem(name: "TrimCode", value: "$MT314"),
                     URLQueryItem(name: "model", value: "m3"),
                     URLQueryItem(name: "condition", value: "new"),
                     URLQueryItem(name: "arrangeby", value: "Price"),
                     URLQueryItem(name: "order", value: "asc"),
                     URLQueryItem(name: "market", value: "CA"),
                     URLQueryItem(name: "language", value: "en"),
                     URLQueryItem(name: "super_region", value: "north america"),
        ]

以上返回如下网址,不正确。

https://www.yoururl.com/api/v1/inventory-results?TrimCode=$MT314&model=m3&condition=new&arrangeby=Price&order=asc&market=CA&language=en&super_region=north%20america

【问题讨论】:

  • 数组在哪里?
  • 以上可能不是正确的措辞。也许你会称它为“查询”对象?

标签: swift get postman urlcomponents


【解决方案1】:
/inventory-results?query={"query":{"model":"xyz","condition":"new","options":{},"arrangeby":"Price","order":"asc","market":"CA","language":"en","super_region":"north america"}}

如果 URL 有效,则表示接受数据作为查询参数,您不能决定将查询参数作为表单数据或其他内容发送。服务器决定如何接收数据。所以看起来服务器只接受数据作为查询参数

你可以做的是用变量替换内容

/inventory-results?query={{data}}

现在在预先请求中做:

 let data = {
  "query": {
    "model": "xyz",
    "condition": "new",
    "options": {},
    "arrangeby": "Price",
    "order": "asc",
    "market": "CA",
    "language": "en",
    "super_region": "north america"
  }
}

//make some changes if you want to data and then

pm.variables.set("data", JSON.stringify(data))

哪个迅速:

    var urlComponents = URLComponents()
    urlComponents.scheme = "https"
    urlComponents.host   = "www.yoururl.com"
    urlComponents.path   = "/api/v1/inventory-results"
    
    
    let query = [
                 URLQueryItem(name: "query", value: "{\"query\":{\"model\":\"xyz\",\"condition\":\"new\",\"options\":{},\"arrangeby\":\"Price\",\"order\":\"asc\",\"market\":\"CA\",\"language\":\"en\",\"super_region\":\"north america\"}}")
         ]

【讨论】:

  • 如何使用 URLComponets 构建这个 URL pin Swift?我知道这可能与标题所暗示的不同。
  • 感谢您的建议
【解决方案2】:

我认为您的想法是正确的,但您希望保持对象表示法样式,您可能希望对此位置字符串进行 URL 编码,因为其中一些字符将通过浏览器行为进行更改。下面是一个例子:

/inventory-results?model=xyz&condition=new&arrangeby=Price&eorder=asc&market=CA&language=en&super_region=north america

我还认为,在这种情况下,最好将所有这些编码为 GET 变量,而不是尝试通过一个巨大的对象进行调用。不确定您使用的是什么语言,但如果处理得当,他们中的大多数会完成此请求并为您制定适当的调用。

由于您没有传递任何密钥或私有数据,因此将其保存在对象符号中最终会对您不利

【讨论】:

  • 我抓取了 Charles 提供的 URL 和结果,以查看网站查询背后的内容。我想把它去掉并与 Postman 一起玩,这就是查询看起来像这样的原因。我真的无法按照建议进行更改。
猜你喜欢
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 2020-06-15
相关资源
最近更新 更多