You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postman中如何以Form-data形式传递数组?Swift 5.x中如何使用URLComponents构建指定格式的API请求URL?

Hey there! Let's tackle your two questions one by one, step by step.

一、在Postman中配置目标格式的请求参数

Your original URL has a query parameter that holds a full JSON object — so this isn't about "splitting an array" but correctly passing that JSON as a query value. Here's how to set it up in Postman:

  • Create a new GET request, and fill in your base URL (like https://www.yoururl.com/inventory-results)
  • Switch to the Params tab
  • In the Key column, type query
  • In the Value column, paste the full JSON string:
    {"query":{"model":"xyz","condition":"new","options":{},"arrangeby":"Price","order":"asc","market":"CA","language":"en","super_region":"north america"}}
    
  • Postman will automatically URL-encode the JSON string for you, so you can just hit send and it'll work as expected.

If you really want to use form-data (note: this is typically for POST requests, not GET), you can switch to a POST method, go to the Body tab, select form-data, and add each field individually (model, condition, arrangeby, etc.). But keep in mind this changes the request format entirely — make sure your backend supports accepting parameters this way first.

二、用Swift 5.x的URLComponents构建目标格式的URL

Your current code is adding each field as a separate query item, but what you need is to pack all those fields into a single JSON string and pass it as the value of the query parameter. Here's the corrected code:

import Foundation

var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "www.yoururl.com"
urlComponents.path = "/api/v1/inventory-results"

// First build the inner dictionary with all your parameters
let innerQueryDict: [String: Any] = [
    "TrimCode": "$MT314",
    "model": "m3",
    "condition": "new",
    "arrangeby": "Price",
    "order": "asc",
    "market": "CA",
    "language": "en",
    "super_region": "north america"
]

// Wrap it in the outer "query" key to match your original URL structure
let outerQueryDict = ["query": innerQueryDict]

// Convert the dictionary to a JSON string
do {
    let jsonData = try JSONSerialization.data(withJSONObject: outerQueryDict, options: [])
    guard let queryJsonString = String(data: jsonData, encoding: .utf8) else {
        print("Failed to convert JSON data to string")
        return
    }
    
    // Add the single query item with the JSON string as its value
    urlComponents.queryItems = [URLQueryItem(name: "query", value: queryJsonString)]
    
    // Check and print the final URL
    if let finalUrl = urlComponents.url {
        print(finalUrl.absoluteString)
    }
} catch {
    print("Error creating JSON string: \(error.localizedDescription)")
}

What this does:

  • We first create a dictionary with all your inner parameters, then wrap it in another dictionary with the key query (to match your original URL's structure)
  • Convert that nested dictionary to a valid JSON string
  • Add that string as the value of a single query URL query item
  • URLComponents will automatically handle URL encoding for special characters, so the final URL will be properly formatted.

内容的提问来源于stack exchange,提问作者Paul S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 18:42:41