Postman中如何以Form-data形式传递数组?Swift 5.x中如何使用URLComponents构建指定格式的API请求URL?
Hey there! Let's tackle your two questions one by one, step by step.
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
Keycolumn, typequery - In the
Valuecolumn, 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.
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
queryURL query item URLComponentswill automatically handle URL encoding for special characters, so the final URL will be properly formatted.
内容的提问来源于stack exchange,提问作者Paul S.

