title: Alamofire 4 Swift 3 GET request with parameters
body: <p>I'm building a network stack using Alamofire 4 and Swift 3. Following the Alamofire guidelines I've created a router for the endpoints of the services. I'm currently using the free API of OpenWeatherMap but I'm finding problems in order to create a get request.
  That's the url needed: <a href="http://api.openweathermap.org/data/2.5/weather?q=Rome&amp;APPID=MY_API_KEY" rel="nofollow noreferrer">http://api.openweathermap.org/data/2.5/weather?q=Rome&amp;APPID=MY_API_KEY</a>. Pasted on a browser, and using a real API Key it works and gives me back my nice json full of info about the weather in the given location.
  On my App I can insert the parameters as Dictionary but I cannot find a way to append the api key at the end of the url.</p>
  
  <p>That's my enum router:</p>
  
  <pre><code>enum OWARouter: URLRequestConvertible {
        case byCityName(parameters: Parameters)
  
  // MARK: Url
      static let baseURLString = "http://api.openweathermap.org"
      static let apiKey = "MY_APY_KEY"
      static let pathApiKey = "&amp;APPID=\(apiKey)"  
      var method: HTTPMethod {
          switch self {
          case .byCityName:
              return .get
          }
      }
  
      var path: String {
          switch self {
          case .byCityName:
              return "/data/2.5/weather"
          }
      }
  
  // MARK: URLRequestConvertible
      func asURLRequest() throws -&gt; URLRequest {
          let url = try OWARouter.baseURLString.asURL()
          var urlRequest = URLRequest(url: url.appendingPathComponent(path))
          switch self {
          case .byCityName(let parameters):
              urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
              print((urlRequest.url)!)
  
          }
  
          urlRequest.httpMethod = method.rawValue
          return urlRequest
       }
      }
  </code></pre>
  
  <p>When I log my (urlRequest.url)! I have this: <a href="http://api.openweathermap.org/data/2.5/weather?q=Rome" rel="nofollow noreferrer">http://api.openweathermap.org/data/2.5/weather?q=Rome</a> but I cannot find a way to add the apiKey. 
  What am I doing wrong?</p>
  
  <p>I've also made an ugly test adding this code after the print:</p>
  
  <pre><code>        var urlRequest2 = URLRequest(url: (urlRequest.url)!.appendingPathComponent(OWARouter.pathApiKey))
          print("URL2: \(urlRequest2)")
  </code></pre>
  
  <p>And the log is URL2: <a href="http://api.openweathermap.org/data/2.5/weather/&amp;APPID=My_API_KEY?q=Rome" rel="nofollow noreferrer">http://api.openweathermap.org/data/2.5/weather/&amp;APPID=My_API_KEY?q=Rome</a>
  How come the api key is in the middle?</p>
  
  <p>If you need this is the simple request code:</p>
  
  <pre><code>   Alamofire.request(OWARouter.byCityName(parameters: ["q":"Rome"])).responseJSON { response in
  
             print(response.request)
             print(response.response)
             print(response.data)
             print(response.result)
  
             debugPrint(response)
  
             if let JSON = response.result.value {
                     print("json: \(JSON)")
             }
          }
  </code></pre>
  
  <p>Another question...
  If I use as parameters ["q":"Rome, IT"], my output url is: <a href="http://api.openweathermap.org/data/2.5/weather?q=Rome%2CIT" rel="nofollow noreferrer">http://api.openweathermap.org/data/2.5/weather?q=Rome%2CIT</a></p>
  
  <p>How to keep the comma?</p>
  
  <p>Thank you!</p>