如何将数组作为表单数据传递给 Postman

How to pass array to Postman as form-data

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

完整 URL

/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"),
        ]

上面returns下面URL不正确

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

我认为您的想法是正确的,但您想要保持对象表示法风格,您可能想要对该位置字符串进行 URLencode,因为其中一些字符将通过浏览器行为发生变化。下面是一个例子:

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

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

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

/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))

在swift中:

    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\"}}")
         ]