Alamofire request with JSONEncoding giving an error - ios

I am getting an error while passing JSON object as a parameter in Alamofire.request
func apiCall2(anyBaseUrl: String = "", apiFunction: ApiName, method m: HTTPMethod = .get,
parameters: [String: Any]? = nil, isJsonEncoding: Bool = false,
completion: #escaping (Bool, Any?, Error?) -> Void) {
var requestUrl = "\(ActiveBaseURL())\(apiFunction.rawValue)"
if anyBaseUrl != "" {
requestUrl = "\(anyBaseUrl)\(apiFunction.rawValue)"
}
var param = [String: Any]()
if let p = parameters {
param = p
}
Alamofire.request(requestUrl, method: m, parameters: param, encoding: isJsonEncoding ? JSONEncoding.default: URLEncoding.default)
.authenticate(user: ApiUserName, password: ApiPassword)
.responseJSON(options: .allowFragments) { response in
print("request body: \(response.request?.httpBody)")
if response.result.isSuccess {
}
print(response.result.error?.localizedDescription)
completion(false, nil, response.result.error)
}
}
My actual JSON format is
{ "discount_code" : "", "giftcard_code" : "", "dt" :
"2018-11-15", "shipping" : {
"state" : "sel",
"name" : "Test",
"address1" : "testevhsd",
"city" : "test city",
"address2" : "testfhd",
"mobile" : "89464633",
"shipment_code" : "YES",
"postal" : "672673",
"country_id" : "129" }, "iccid" : "", "uid" : "3592", "product" : [
{
"id" : 104,
"type" : "countryPlan",
"qty" : 1
} ] }
And this is how I am passing the JSON in function
var parms: Parameters {
return [
"uid": "3592",
"product": [
["id": 104,"type":"countryPlan", "qty": 1]
],
"shipping": ["name":"Test", "mobile": "89464633", "address1": "testevhsd",
"address2": "testfhd", "city": "test city", "state": "sel",
"postal": "672673", "shipment_code": "YES", "country_id": "129"],
"discount_code": "",
"dt":"2018-11-15",
"giftcard_code": "",
"iccid": ""]
}
Service.shared.apiCall2(apiFunction: .CheckOut, method: .post, parameters: parms, isJsonEncoding: true) { (success, data, _) in
My Error
"JSON could not be serialized because of error:\nThe data couldn’t be read because it isn’t in the correct format."
Initially I thought this is because of web service not able to return the correct JSON but I was wrong if I run same service from postman it is working fine and I am getting data in JSON format
I feel the error is because for JSON data passing

Related

Send JSON with array of objects as parameter in Alamofire with swift

I am very new with Swift and Alamofire, what I want to accomplish is to send a data structure, like this:
{
"name" : "Test name",
"intention" : "Purpose of practice test",
"id_frequency" : "1",
"member": [
{
"id_member" : "1",
"email" : "member1#gmail.com",
"id_member_type" : 1
},
{
"id_member" : "4",
"email" : "member2#gmail.com",
"id_member_type" : 3
},
{
"id_member" : "7",
"email" : "member3#gmail.com",
"id_member_type" : 3
},
{
"id_member" : "5",
"email" : "member4#gmail.com",
"id_member_type" : 3
},
{
"id_member" : "6",
"email" : "member5#gmail.com",
"id_member_type" : 3
}
]
}
The way I am proceeding to structure the desired json, is as follows:
var membersArray = [AnyObject]()
for i in 0..<members.count {
let json: [String: Any] = [
"id_member": members[i].idMember!,
"email": members[i].email!,
"id_member_type": "\(Int(members[i].idMemberType)!)",
]
membersArray.append(json as AnyObject)
}
let jsonMembers = JSON(membersArray)
let jsonObject: [String: Any] = [
"member" : jsonMembers,
"name": name!,
"intention": intention!,
"id_frequency": frequency!
]
let jsonUpdate = JSON(jsonObject)
With this structured json, lines above (jsonUpdate). I proceed to execute the webService.
WevServices.createRequest(requestInfo: jsonUpdate) { (result) in
print(result)
}
My webservice method, looks like this:
static func createRequest(requestInfo: JSON, completion: #escaping (_ result: String) -> Void){
let url = URL(string: "http://ws.someURL.com/CreateRequest")
let parameters: [String : Any] = ["name" : "\(requestInfo["name"])", "intention" : "\(requestInfo["intention"])", "id_frequency" : "\(requestInfo["id_frequency"])", "member": requestInfo["member"]]
Alamofire.request(url!, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).responseString { response in
print(response)
}
}
I'm get an error from the server that the send data would not be correct.
Note: My application use Lumen as backend.
The best way for doing what you need is :
func JSONToString(json: [String : String]) -> String?{
do {
let mdata = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // json to the data
let convertedString = String(data: mdata, encoding: String.Encoding.utf8) // the data will be converted to the string
print("the converted json to string is : \(convertedString!)") // <-- here is ur string
return convertedString!
} catch let myJSONError {
print(myJSONError)
}
return ""
}
and for alamofire request do this :
static func createRequest(requestInfo: [String :String], completion: #escaping (_ result: String) -> Void){
let url = URL(string: "http://ws.someURL.com/CreateRequest")
let parameters: [String : Any] = ["request" : JSONToString(json : requestInfo)!]
Alamofire.request(url!, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).responseString { response in
print(response)
}

How to parsed all the data in JSON?

First issue I addressed
I am working on an APIService using Alamofire, I tried to print the response and the I got the data successfully, but unfortunately the data from JSON turns to nil when I parse it to the attendees Object. How can I reflect the data from json to the attendees object?
Second Issue
I solved the 1st issue, after all the debugging I had. The codes I used was written in my answer below. I parsed data from JSON going to attendees but as I checked only the first array was fetch. How can I get all the data inside the JSON? Hope you can help me. Thank you.
func getParticipants(passcode: String,
participantType: ParticipantType,
successBlock: #escaping (Attendees?) -> Void,
failureBlock: #escaping (Error) -> Void)
{
let attendeesURL = URL(string: "\(GET_PARTICIPANTS_URL)/\(passcode)/\(participantType)")
Alamofire.request(attendeesURL!, method: .get).responseJSON { (response) in
print(response)
if let error = response.error
{
failureBlock(error)
return
}
if let attendeeJSON = response.result.value as? [Dictionary<String, Any>],
let attendeeObj = attendeeJSON.first {
print(attendeeObj)
let attendees = Attendees.init(JSON: attendeeObj)
successBlock(attendees)
}
}
}
}
JSON
[
{
"event_name": "Laugh Trip",
"event_participants": [
{
"participant_id": "6f1e7fd5-6da9-4d5b-bc91-4771aeaa5235",
"employee_number": "",
"last_name": "name",
"first_name": "name",
"middle_name": "",
"display_name": "name, name ",
"department_name": "IT",
"position_name": "Application Developer",
"registered_flag": true,
"registered_datetime": "2018-07-16T14:51:57.813",
"registration_type": 1,
"delete_flag": false,
"manual_reg_flag": false,
"out_flag": true,
"out_datetime": "2018-07-16T14:54:00.000",
"classification": 1,
"others": ""
},
{
"participant_id": "6f1e7fd5-6da9-4d5b-bc91-4771aeaa5235",
"employee_number": "",
"last_name": "name",
"first_name": "name",
"middle_name": "",
"display_name": "name, name ",
"department_name": "IT",
"position_name": "Application Developer",
"registered_flag": true,
"registered_datetime": "2018-07-16T14:51:57.813",
"registration_type": 1,
"delete_flag": false,
"manual_reg_flag": false,
"out_flag": true,
"out_datetime": "2018-07-16T14:54:00.000",
"classification": 1,
"others": ""
},
]
]
Instead of using first which gets only the first item of the sequence use a loop respectively.
if let events = response.result.value as? [[String : Any]] {
for event in events {
if let eventparticipants = event["event_participants"] as? [[String : Any]] {
print(eventparticipants)
for participant in eventparticipants {
let attendees = Attendees.init(JSON: participant)
successBlock(attendees)
}
}
}
}
I recommend to decode the JSON directly into structs with Decodable
I solved my own issue. :D
Alamofire.request(attendeesURL!, method: .get).responseJSON { (response) in
print(response)
if let error = response.error
{
failureBlock(error)
return
}
if let jsonDictionary = response.result.value as? [Dictionary<String, Any>]{
if let eventparticipants = jsonDictionary.first {
print(eventparticipants)
if let partObj = eventparticipants["event_participants"] as? [[String : Any]]{
let attendeeObj = partObj.first
let attendees = Attendees.init(JSON: attendeeObj!)
successBlock(attendees)
}
}
}
}

Send multiple array in parameter in swift

I'm calling a service in which I'm trying to send parameter that consist of array within an array and other parameters which are outside from an array. The format in which I have to send parameter is this:
{
"RestID": 0,
"cart": [
{
"childs": [
{
"addon_cat_id": 0,
"id": 0,
"name": "string",
"next_move_id": 0,
"price": 0,
"sort_order": 0,
"type": "string"
}
],
"name": "string",
"price": "string",
"productid": 0,
"qty": 0,
"description": "string"
}
],
"coupon_code": "string",
"coupon_type": "string",
"coupon_value": 0,
"delivery_price": "string",
"discount_amount": "string"
}
This is how I'm sending my parameter in the service:
let parameter:[String:Any] = ["RestID":restaurantId!,
"cart":[["childs":["addon_cat_id":"0",
"id":ItemDataSource.sharedInstance.items[0].itemId!,
"name":ItemDataSource.sharedInstance.items[0].itemName!,
"next_move_id":"",
"price":ItemDataSource.sharedInstance.items[0].itemPrice!,
"sort_order":"",
"type":"string"
]],
["name": ItemDataSource.sharedInstance.items[0].itemName!,
"price": ItemDataSource.sharedInstance.items[0].itemPrice!,
"productid": ItemDataSource.sharedInstance.items[0].itemId!,
"qty": 2,
"description":ItemDataSource.sharedInstance.items[0].itemDdescription!]
],
"coupon_code" : couponCode!,
"coupon_type" : couponType!,
"coupon_value" : couponValue!,
"delivery_price" : deliveryLbl.text!,
"discount_amount" : disocunt!,
"discount_description" : discountDesc!,
"discount_info" : discountInfo!,
"distance": "",
"door_num" : doorTxt.text!,
"firstname" : "",
"lastname" : "",
"order_type" : "Delivery",
"payment_mode" : "",
"phone" : mobileTxt.text!,
"postcode" : postCodeTxt.text!,
"preorder" : true,
"preorder_is_preorder" : "",
"street" : address1Txt.text!,
"token" : "",
"town" : addressTxt2.text!,
"total": self.grandTotalLbl.text!,
"stripeToken" : "",
"customer_id" : "2",
"preordertime": preOrderTime,
"usercmt": "descri",
"email": userEmail!
]
But when I call the service I get an error. I have discussed this with the server side and they are saying it is a mistake in sending the parameter. How can I send multiple arrays within an array in parameter?
This is my service class:
class SaveOrderDeliveryService{
static let instance = SaveOrderDeliveryService()
var status:Int = 0
func saveOrderDelivery(param:[String:Any],completion:#escaping CompletionHandler) {
Alamofire.request(saveOrderDeliveryUrl, method: .post, parameters:param, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)")// response serialization result
print(response)
if response.result.error == nil{
self.status = (response.response?.statusCode)!
if(self.status == 200){
print(response)
guard let data = response.data else {return}
do{
if let json = try JSON(data: data).dictionary{
completion(true)
}
}catch let jsonErr{
print(jsonErr)
}
}
completion(true)
}else{
completion(false)
debugPrint(response.result.error as Any)
}
}
}
}
Try this add your data , make sure now one with nil
let parameters = [
"RestID": "",
"cart": [
[
"childs": [
[
"addon_cat_id": "",
"id": "",
"name": "string",
"next_move_id": "",
"price": "",
"sort_order": "",
"type": "string"
]
],
"name": "string",
"price": "string",
"productid": "",
"qty": "",
"description": "string"
]
],
"coupon_code": "string",
"coupon_type": "string",
"coupon_value": "",
"delivery_price": "string",
"discount_amount": "string"
] as [String : Any]
Alamofire.request(saveOrderDeliveryUrl, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
}

Alamofire .post api error: Code=3840 "Invalid value around character 1

Using Alamofire for .post api, api giving data in postman but not in my code. Posting below the code, please guide what wrong i am doing here:
// API calling method:
parameters = [
"Address" : "" as AnyObject,
"Name" : "" as AnyObject,
"ServiceID" : "" as AnyObject,
"Rating" : "" as AnyObject,
"Price" : "" as AnyObject
]
let headers: Dictionary = [
"" : ""
]
print(parameters)
ApiServices.requestPOSTURL(strURL, params: parameters, headers: headers, success:{
(JSONResponse) -> Void in
CommonMethodsClass.hideHUD(targetView: self.view)
print(JSONResponse["message"])
let strMsg = JSONResponse["message"].stringValue
if (JSONResponse["status"].intValue == 1)
{
}
else
{
CommonMethodsClass.showAlertMessage(vc: self, titleStr: "Error!", messageStr: strMsg)
}
}) {
(error) -> Void in
print(error)
CommonMethodsClass.hideHUD(targetView: self.view)
}
// Api request method:
class func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, headers : [String : String]?, success:#escaping (JSON) -> Void, failure:#escaping (Error) -> Void){
Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default).responseJSON { (responseObject) -> Void in
print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value as Any)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
Error: FAILURE:
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error
Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character
1." UserInfo={NSDebugDescription=Invalid value around character 1.}))
Update: Response to parse, may also needs to be changed.
{
"status": true,
"message": "",
"data": [
{
"SalonID": "1",
"SalonName": "Affinity",
"SalonEmail": "vay.chaan#th-rce.com",
"SalonPhone": "9999888877",
"SalonMobile": "9999888877",
"SalonAddress": "C-28, Sec-58, India",
"Latitude": "18.5806",
"Longitude": "27.36273",
"Image": null,
"SalonImage": "",
"TimeIntervalminutes": 20,
"AverageRating": 4,
"IsActive": 1
},
{
"SalonID": "3",
"SalonName": "Looks",
"SalonEmail": "rad#th-rce.com",
"SalonPhone": "99998828877",
"SalonMobile": "99998388877",
"SalonAddress": "GP Mall,India",
"Latitude": "",
"Longitude": "",
"Image": null,
"SalonImage": "",
"TimeIntervalminutes": 30,
"AverageRating": 5,
"IsActive": 1
}
]
}
Replace responseJSON with responseString.
You need to convert the string for values. Can you update the string in your question?
You can use this function to convert response string into JSON:
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
String: let str = "{\"name\":\"James\"}"
USAGE: let dict = convertToDictionary(text: str)
Try responseString instead of responseJSON and it will work
Alamofire.request("URL").responseString { response in
print(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}
Change responseJSON to responseString and it will work :)
Alamofire.request("URL").responseString { response in
print(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}

Alamofire Parameters - NSDictionary is not convertible to [String : AnyObject]

So I am trying to send a json object as parameters with Alamofire. I think I have followed example in their documentation but I get this error with the method call.
#IBAction func saveButton(sender: AnyObject) {
var url = "http://b857aaa4.ngrok.io/recruit"
let parameters = [
"_id" : firstNameTextField.text,
"alt_email" : "",
"forename" : firstNameTextField.text,
"phone" : recruteePhone.text,
"recruiter" : [
"comments" : "",//TODO comments
"date_met" : "",//TODO tdoays date
"email" : "",//TODO recruiter email
"event_name" : ""//TODO event name
],
"surname" : lastNameTextField.text
]
Alamofire.request(.PUT, url, parameters: parameters, encoding: .JSON)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)\n")
} else {
var json = JSON(json!);
println("success");
}
}
Declare parameters as
let parameters: [String: AnyObject] = [
and it compiles okay.

Resources