首页 > 解决方案 > 如何将数组作为表单数据传递给邮递员

问题描述

我有以下 URL,我想在 Postman 中进行测试。但是我想分解数组以便于测试,例如作为表单数据(或其他)。我将如何在 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

标签: swiftgetpostmanurlcomponents

解决方案


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

推荐阅读