Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I build a URL with query parameters containing multiple values for the same key in Swift?

I am using AFNetworking in my iOS app and for all the GET requests it makes, I build the url from a base URL and than add parameters using NSDictionary Key-Value pairs.

The problem is that I need same key for different values.

Here is an example of what I need the finally URL to look like -

http://example.com/.....&id=21212&id=21212&id=33232

It's not possible in NSDictionary to have different values in same keys. So I tried NSSet but did not work.

let productIDSet: Set = [prodIDArray] let paramDict = NSMutableDictionary() paramDict.setObject(productIDSet, forKey: "id") 
like image 452
AbhishekDwivedi Avatar asked Dec 03 '15 08:12

AbhishekDwivedi


People also ask

How can URL have multiple query parameters?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

How do I add parameters to a URL query?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

How many query string parameters can be added onto a URL?

Although officially there is no limit specified by RFC 2616, many security protocols and recommendations state that maxQueryStrings on a server should be set to a maximum character limit of 1024.


2 Answers

All you need is URLComponents (or NSURLComponents in Obj-C). The basic idea is to create a bunch of query items for your id's. Here's code you can paste into a playground:

import Foundation import XCPlayground  let queryItems = [URLQueryItem(name: "id", value: "1"), URLQueryItem(name: "id", value: "2")] var urlComps = URLComponents(string: "www.apple.com/help")! urlComps.queryItems = queryItems let result = urlComps.url! print(result) 

You should see an output of

www.apple.com/help?id=1&id=2

like image 193
Daniel Galasko Avatar answered Oct 14 '22 11:10

Daniel Galasko


Method 1

It can add the QueryItem to your existing URL.

extension URL {      func appending(_ queryItem: String, value: String?) -> URL {          guard var urlComponents = URLComponents(string: absoluteString) else { return absoluteURL }          // Create array of existing query items         var queryItems: [URLQueryItem] = urlComponents.queryItems ??  []          // Create query item         let queryItem = URLQueryItem(name: queryItem, value: value)          // Append the new query item in the existing query items array         queryItems.append(queryItem)          // Append updated query items array in the url component object         urlComponents.queryItems = queryItems          // Returns the url from new url components         return urlComponents.url!     } } 

How to use

var url = URL(string: "https://www.example.com")! let finalURL = url.appending("test", value: "123")                   .appending("test2", value: nil) 

Method 2

In this method, the URL will be updated automatically.

extension URL {      mutating func appendQueryItem(name: String, value: String?) {          guard var urlComponents = URLComponents(string: absoluteString) else { return }          // Create array of existing query items         var queryItems: [URLQueryItem] = urlComponents.queryItems ??  []          // Create query item         let queryItem = URLQueryItem(name: name, value: value)          // Append the new query item in the existing query items array         queryItems.append(queryItem)          // Append updated query items array in the url component object         urlComponents.queryItems = queryItems          // Returns the url from new url components         self = urlComponents.url!     } }  // How to use var url = URL(string: "https://www.example.com")! url.appendQueryItem(name: "name", value: "bhuvan") 
like image 44
Bhuvan Bhatt Avatar answered Oct 14 '22 12:10

Bhuvan Bhatt