Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a POST request with BODY in swift

I'm trying to make a post request with a body in swift using Alamofire.

my json body looks like :

{     "IdQuiz" : 102,     "IdUser" : "iosclient",     "User" : "iosclient",     "List":[         {         "IdQuestion" : 5,         "IdProposition": 2,         "Time" : 32         },         {         "IdQuestion" : 4,         "IdProposition": 3,         "Time" : 9         }     ] } 

I'm trying to make let list with NSDictionnary which look like :

[[Time: 30, IdQuestion: 6510, idProposition: 10], [Time: 30, IdQuestion: 8284, idProposition: 10]] 

and my request using Alamofire looks like :

Alamofire.request(.POST, "http://myserver.com", parameters: ["IdQuiz":"102","IdUser":"iOSclient","User":"iOSClient","List":list ], encoding: .JSON)             .response { request, response, data, error in             let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)                 println(dataString)         } 

The request has an error and I believe the problem is with Dictionary list, cause if I make a request without the list it works fine, so any idea ?


I have tried the solution suggested but I'm facing the same problem :

 let json = ["List":list,"IdQuiz":"102","IdUser":"iOSclient","UserInformation":"iOSClient"]         let data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted,error:nil)         let jsons = NSString(data: data!, encoding: NSUTF8StringEncoding)        Alamofire.request(.POST, "http://myserver.com", parameters: [:], encoding: .Custom({         (convertible, params) in         var mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest         mutableRequest.HTTPBody = jsons!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)         return (mutableRequest, nil)     }))         .response { request, response, data, error in         let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)            println(dataString)     } 
like image 450
Stranger B. Avatar asked Aug 13 '15 07:08

Stranger B.


People also ask

How do I send a body in a post request?

The body format is defined by the Content-Type header. When using a HTML FORM element with method="POST" , this is usually application/x-www-form-urlencoded . Another very common type is multipart/form-data if you use file uploads.

How do you post an HTTP request with a JSON body in Swift?

var request = URLRequest(url: urlUser) request. httpMethod = "POST" request. setValue("application/json", forHTTPHeaderField: "Content-Type") var responseStatus = 0 let task = URLSession. shared.


1 Answers

If you are using Alamofire v4.0+ then the accepted answer would look like this:

let parameters: [String: Any] = [     "IdQuiz" : 102,     "IdUser" : "iosclient",     "User" : "iosclient",     "List": [         [             "IdQuestion" : 5,             "IdProposition": 2,             "Time" : 32         ],         [             "IdQuestion" : 4,             "IdProposition": 3,             "Time" : 9         ]     ] ]  Alamofire.request("http://myserver.com", method: .post, parameters: parameters, encoding: JSONEncoding.default)     .responseJSON { response in         print(response)     } 
like image 76
Gianni Carlo Avatar answered Sep 26 '22 20:09

Gianni Carlo