swift - 如何将数组作为表单数据传递给邮递员
问题描述
我有以下 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
解决方案
/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\"}}")
]
推荐阅读
- r - 按 R 中的特定日期范围过滤
- ios - 如果 iOS App Clip 的 App 已经具备推送通知功能,它是否需要它?
- python - 为什么 PyShark 会在超时的情况下继续 LiveCapture?
- git - 如何将本地文件夹推送到新的 Github 存储库?
- sharepoint - 在从模板提供共享点时调用 ClientContext.Web.ApplyProvisioningTemplate() 时出现 Web 异常
- python-3.x - Folium 和 Geopandas 对象
- canvas - 调整画布大小,保持内容比例,避免拉伸
- c# - C# 思考:如何获取泛型对象的类型
- javascript - Js 函数应该返回一个特定的字符串,但行为怪异
- java - 与 Swing GUI 的 Java 套接字连接冻结