How to Add Alamofire Url Parameters

How to add Alamofire URL parameters

The problem is that you're using URLEncoding.default. Alamofire interprets URLEncoding.default differently depending on the HTTP method you're using.

For GET, HEAD, and DELETE requests, URLEncoding.default encodes the parameters as a query string and adds it to the URL, but for any other method (such as POST) the parameters get encoded as a query string and sent as the body of the HTTP request.

In order to use a query string in a POST request, you need to change your encoding argument to URLEncoding(destination: .queryString).

You can see more details about how Alamofire handles request parameters here.

Your code should look like:

   _url = "http://localhost:8080/"
let parameters: Parameters = [
"test": "123"
]

Alamofire.request(_url,
method: .post,
parameters: parameters,
encoding: URLEncoding(destination: .queryString),
headers: headers)

Alamofire - add both query parameter and body

Alamofire's URLEncoding has an initializer that gets destination as parameter. Based on documentation:

Destination defining where the encoded query string will be applied.
.methodDependent by default.

By default .methodDependent case checks the HTTP method of the request and only on .get, .head and .delete requests it encodes the passed parameters in the URL.

Given that fact, you never see paramA in your request (not even in the body) because you override it in the following line:

// here body is already serialized to Json
request.httpBody = Data(bodyInJsonString.utf8)

So, instead of using the default URLEncoding, you can simply call URLEncoding's initializer directly passing as destination the .queryString case like this:

// here I am specifying `paramA` value
request = try URLEncoding(destination: .queryString).encode(request, with: parameters)

// here body is already serialized to Json
request.httpBody = Data(bodyInJsonString.utf8)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

That way you will have a request with paramA in the URL and your JSON as a body.

Encode URL before using in Alamofire

Instead of writing your own URL "in hand" you can use URLComponents to ease adding URL parameters and so on.

Here is an example using your URL from above:

var apiKey = "key-goes-here"
var amount = 10
var price = 20
var urlParameters = URLComponents(string: "https://google.com/")!
urlParameters.path = "/accounts"

urlParameters.queryItems = [
URLQueryItem(name: "apiKey", value: apiKey),
URLQueryItem(name: "market", value: "USD"),
URLQueryItem(name: "quantity", value: "\(amount)"),
URLQueryItem(name: "rate", value: "\(price)"),
URLQueryItem(name: "nonce", value: "\(Date().timeIntervalSince1970)")
]

urlParameters.url //Gives you a URL with the value https://google.com/accounts?apiKey=key-goes-here&market=USD&quantity=10&rate=20&nonce=1513630030.43938

Granted, it does not make your life that much easier as you still have to write the URL yourself, but at least you don't have to wrestle with adding & and ? in the correct order anymore.

Hope that helps you.

Parameters can't be passed to a url using Alamofire

As I looked now into RapidApi documentation for API-Football, you don't need the parameters. You just need to construct the URL from different parts and then make a GET request.

class ViewController: UIViewController {

let MAIN_URL = "https://api-football-v1.p.rapidapi.com/v2/leagues/country/"

override func viewDidLoad() {
super.viewDidLoad()

getLeague(for: "england", year: 2018)
}

func getHeaders() -> [String : String] {
let headers = [
"X-RapidAPI-Host": "api-football-v1.p.rapidapi.com",
"X-RapidAPI-Key": "xxxxxxxxxxxxxxxxxxx"
]
return headers
}

func getLeague (for country : String, year: Int) {

let url = MAIN_URL + country + "/\(year)/"

Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: getHeaders()).responseJSON {
response in
if response.result.isSuccess {

let leagueJSON : JSON = JSON(response.result.value!)
print(leagueJSON)
print()
}
else {

}
}
}
}

Alamofire: Send multiple URL encoded parameters with same key

It's not possible to create dict like that in iOS, instead, you can do multiple API calls.



Related Topics



Leave a reply



Submit