0

i have created one common class for alamofire request i want to send parameters as aplication/x-www-form-urlencoded how to add the parameters to my urlRequest

i have managed to add parameters as application/json to urlRequest using below code

    do {
            urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])
        } catch {
            throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
        }

i need something similar for aplication/x-www-form-urlencoded

here is my parameters

case .ewalletData :
        return [K.APIParameterKey.token :"OP8JHOEOZ5KJW1X",K.APIParameterKey.fromMobile:"true",K.APIParameterKey.adminName:"binaryecom",K.APIParameterKey.limit:"100",K.APIParameterKey.offset:"0",K.APIParameterKey.userName:"OC6DGH"]
Midhun Narayan
  • 829
  • 2
  • 9
  • 26

2 Answers2

2

here is the code it works for me in swift 4: let postString = "your parameter="+value+"&your parameter="+value request.httpBody = postString.data(using: .utf8) let task = URLSession.shared.dataTask(with: request, completionHandler: completionHandle) task.resume()

ZahraAsgharzade
  • 311
  • 3
  • 10
1

try this :

        guard let request_url = URL(string: Url) else { return }
        let parameterDictionary = ["your parameter" : value]
        var request = URLRequest(url: request_url)
        request.httpMethod = "POST"

request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
request.setValue("aplication/x-www-form-urlencoded", forHTTPHeaderField: "String")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: parameterDictionary, options: []) else {
            return
        }
        request.httpBody = httpBody
ZahraAsgharzade
  • 311
  • 3
  • 10