title: How to do parameter encoding in Alamofire 4?
body: <p>having trouble getting my head around parameter encoding in Alamofire 4. Before I would use the <code>ParameterEncoding</code> enumeration and do something like this:</p>
  
  <pre><code>Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0
  </code></pre>
  
  <p>However this has since been replaced with the <code>ParameterEncoding</code> <code>protocol</code>:</p>
  
  <pre><code>public typealias Parameters = [String: Any]
  
  public protocol ParameterEncoding {
      func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -&gt; URLRequest
  }
  </code></pre>
  
  <p>How could I intergrate this into my current code? How do I encode my current <code>mutableURLRequest</code>?</p>
  
  <p>EDIT:</p>
  
  <pre><code>static func urlRequestWithComponents(urlString:String, parameters:Dictionary&lt;String, Any&gt;, data:Data?, contentType: ContentType, parameterName: String) -&gt; (URLRequestConvertible, Data) {
  
          var urlRequest = URLRequest(url: URL(string: urlString)!)
          urlRequest.cachePolicy = .reloadIgnoringLocalCacheData
          urlRequest.httpMethod = Alamofire.HTTPMethod.post.rawValue
          let boundaryConstant = "multipart-boundary";
          let multipartContentType = "multipart/form-data;boundary="+boundaryConstant
          urlRequest.setValue(multipartContentType, forHTTPHeaderField: "Content-Type")
  
          // Create upload data to send
          var uploadData = Data()
  
          if data != nil {
              // Add multipart data
              uploadData.append("\r\n--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)
              if contentType == .Image_JPEG {
                  uploadData.append("Content-Disposition: form-data; name=\"\(parameterName)\"; filename=\"file.jpeg\"\r\n".data(using: String.Encoding.utf8)!)
                  uploadData.append("Content-Type: image/jpeg\r\n\r\n".data(using: String.Encoding.utf8)!)
              } else if contentType == .Audio {
                  let dateFormatter = DateFormatter()
                  dateFormatter.dateFormat = "MMyyyyddsshhmmA" // superset of OP's format
                  let audioFileName = "recording-\(dateFormatter.string(from: Date())).mp4"
                  uploadData.append("Content-Disposition: form-data; name=\"\(parameterName)\"; filename=\"\(audioFileName)\"\r\n".data(using: String.Encoding.utf8)!)
                  uploadData.append("Content-Type: audio/mpeg\r\n\r\n".data(using: String.Encoding.utf8)!)
              }
              uploadData.append(data!)
          }
  
          // Add parameters
          for (key, value) in parameters {
              uploadData.append("\r\n--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)
              uploadData.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".data(using: String.Encoding.utf8)!)
          }
          uploadData.append("\r\n--\(boundaryConstant)--\r\n".data(using: String.Encoding.utf8)!)
  
          // return URLRequestConvertible and Data
          return (urlRequest, uploadData)
      }
  </code></pre>
  
  <p>When doing a request:</p>
  
  <pre><code>let urlRequest = urlRequestWithComponents(urlString: url, parameters: parameters, data: imageData, contentType: .Image_JPEG, parameterName: "question_img")
  
  
      alamoManager.upload(urlRequest.1, with: urlRequest.0).responseObject { (response: DataResponse&lt;CustomResponse&gt;) in
  
       //Some code
      }
  </code></pre>