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

时间:2021-06-09 14:56:58

标签: swift get postman urlcomponents

我想在 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

2 个答案:

答案 0 :(得分: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\"}}")
         ]

答案 1 :(得分:0)

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

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

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

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