Alamofire httpBody show 500 statusCode in swift 5 - ios

I am using Alamofire 5. I have put the parameters to httpbody. I have run it in Postman, and it is working fine. My body parameters are below.
{
"user_id": "user_id",
"username": "user_name",
"password": "pass",
"token": "",
"type_id": 100,
"country_id": 1,
"language_id": 1,
"customer_id": 1,
"parent_user_id": "",
"profile": {}
}
I have used below code to put the request using Alamofire 5. Here is the code:
func change_password() {
let headers: HTTPHeaders = [
"Content-Type": "application/json",
"YumaSession": Global_Variable.globalValue.session_id
]
let parameters:Parameters = [
"user_id": "user_id",
"username": "username",
"password": txtPassword.text!,
"token": "",
"type_id": 100,
"country_id": 1,
"language_id": 1,
"customer_id": 1,
"parent_user_id": "",
"profile": []
]
AF.request( url,method: .put,parameters: parameters,encoding: URLEncoding.httpBody,headers: headers).responseJSON{ response in
switch response.result {
case .success(let responseData):
print("responseData-->",response.response!.statusCode)
case .failure(let error):
print("error--->",error)
}
}
}
Above code returns 500 status code. What is wrong above the code?

parameters.profile is defined with an array literal instead of an dictionary one. Fixed code below.
let parameters:Parameters = [
"user_id": "user_id",
"username": "username",
"password": txtPassword.text!,
"token": "",
"type_id": 100,
"country_id": 1,
"language_id": 1,
"customer_id": 1,
"parent_user_id": "",
"profile": [:]
]

Related

How can I post request json body in swift using Alamofire?

I need to make a POST request in the json body for my app backend but the response returns a failure. I assume my json formatting or encoding is wrong but I can't figure out what the problem is. I have attempted a lot of different solutions but I haven't been able to find one that works. Can anyone see which part of my code is responsible for the failure?
let headers: HTTPHeaders = [
"accept": "application/json",
"content-type": "application/json",
"authorization": self.authValue,
"x-iyzi-rnd": self.randomString,
"cache-control": "no-cache"
]
let parameters: [String: Any] = [
"price": "1.0",
"paidPrice": "1.1",
"paymentChannel": "mobile_ios",
"paymentCard": [
"cardHolderName": "card_name",
"cardNumber": "card_no",
"expireYear": "2030",
"expireMonth": "09",
"cvc": "123"
],
"buyer": [
"id": "123123123",
"name": "john",
"surname": "doe",
"identityNumber": "12345678902",
"email": "johndoe#gmail.com",
"registrationAddress": "nidakulegöztepemerdivenköymahborasokno1",
"city": "istanbul",
"country": "turkey",
"ip": "192.168.1.82"
],
"shippingAddress": [
"address": "nidakulegöztepemerdivenköymahborasokno1",
"contactName": "janedoe",
"city": "istanbul",
"country": "turkey"
],
"billingAddress": [
"address": "nidakulegöztepemerdivenköymahborasokno1",
"contactName": "janedoe",
"city": "istanbul",
"country": "turkey"
],
"basketItems": [
[
"id": "321",
"price": "0.3",
"name": "binocular",
"category1": "collectibles",
"itemType": "physical"
],
[
"id": "432",
"price": "0.5",
"name": "gamecode",
"category1": "game",
"itemType": "virtual"
],
[
"id": "543",
"price": "0.2",
"name": "usb",
"category1": "electronics",
"itemType": "physical"
]
],
"currency": "try"
]
Alamofire.request("https://api.iyzipay.com/payment/auth", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON(completionHandler: {
response in response
let jsonResponse = response.result.value as! NSDictionary
print(jsonResponse)
})
Is there any different way to post request json body?

How to post the nested dictionary as JSON body using swift

{
"data": [{
"_id": "1558946471217677c352281",
"title": "ssds",
"user_id": "",
"user_name": "",
"checklist": {
"id": "5ccad234e47ab8565a45b6a2",
"name": "task edited 2"
},
"idc": "5ccad234e47ab8565a45b6a2",
"completed": 0,
"created": "2019-05-27T22:41:11+14:00",
"tps": 1,
"tpe": 0
}],
"tz": "2d626e1289dafb70b0e890eb97bf5c4910b0a47edad9624b5973cdb244abe8fed5e6",
"uID": "7c24d0c8c6034017556946c418bbf0b182ba6a0a62230a860c3319c8f137c06920da465df3d789d1"
}
You can prepare your dictionary format as follows:
let checklist = [
"id": "5ccad234e47ab8565a45b6a2",
"name": "task edited 2"
]
let data = [
[
"_id": "1558946471217677c352281",
"title": "ssds",
"user_id": "",
"user_name": "",
"checklist": "",
"idc": "5ccad234e47ab8565a45b6a2",
"completed": 0,
"created": "2019-05-27T22:41:11+14:00",
"tps": 1,
"tpe": 0
],
[
"_id": "1558946471217677c352281",
"title": "Abcd",
"user_id": "",
"user_name": "",
"checklist": "",
"idc": "5ccad234e47ab8565a45b6a2",
"completed": 0,
"created": "2019-05-27T22:41:11+15:00",
"tps": 1,
"tpe": 0
]
] as [[String : Any]]
let paramDict = [
"data": [data],
"tz":"2d626e1289dafb70b0e890eb97bf5c4910b0a47edad9624b5973cdb244abe8fed5e6",
"uID":"7c24d0c8c6034017556946c418bbf0b182ba6a0a62230a860c3319c8f137c06920da465df3d789d1"
] as [String : Any]

invalid value around character 0 in swift

I am developing an iOS application where it uses an API to get the user's information. This is the response I am getting back from the server which is a JSON object:
{
"status": {
"code": 200,
"message": "OK",
"error": 0,
"error_messages": []
},
"data": {
"id": 1111,
"name": "xxxxxxxx",
"triple_name": "xxxxxxxx",
"english_name": "xxxxxxxx",
"email": "xxxxxxxx",
"telephone": "xxxxxxxx",
"username": "xxxxxxxx",
"id_type": "xxxxxxxx",
"id_number": "xxxxxxxx",
"id_expiry_date": null,
"id_photo": null,
"gender": "xxxxxxxx",
"date_of_birth": "xxxxxxxx",
"h_date_of_birth": null,
"nationality_id": 0,
"country_id": 0,
"city_id": 0,
"degree_id": null,
"personal_photo": null,
"is_government_employee": 0,
"iban": null,
"bank_id": null,
"access_token": "xxxxxxxx",
"country_name": "xxxxxxxx",
"city_name": "xxxxxxxx"
}
}
However, I am getting an error in this line of code:
let myJSON = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
The error is:
invalid value around character 0
. I have used a JSON validator and it says the JSON is valid. However, i keep getting this error
try changing NSDictonary to [String:AnyObject]
or to [String:Any]

int to double conversion in http request

I'm new to Flutter. While I was trying to hit HTTP request int values in http request are being converted to double values in Flutter. Below is the http response while i'm hitting through flutter service:
{
"GS": 1.0,
"DATA": {
"XXXX": 13,
"XXXX": "Managing",
"XXXX": "F6ACEE86D6E6",
"XXXX": "Santhosh Reddy",
"PHONE": 9.652823823E9,
"EMAIL": "",
"DISPLAYMESSAGE": "Successfull."
},
"RS": 1.0
}
The actual response while checking through serviceUrl is as below:
{
"GS": 1,
"DATA": {
"XXXX": 13,
"XXXX": "Managing",
"XXXX": "F6ACEE86D6E6",
"XXXX": "Santhosh Reddy",
"PHONE": 9652823823,
"EMAIL": "",
"DISPLAYMESSAGE": "Successfull."
},
"RS": 1
}.

Create a dictionary in swift with alamofire

Im trying to create a web request using alamofire in swift , by request object should be like this
{
"warranty": 0,
"descriptions": "string",
"product_name": "string",
"purchase_date": "23/10/2016",
"product_image": "string",
"product_receipt": "string",
"serial_number": "string",
"barcode_image": "string",
"serial_number_image": "string",
"product": {
"id": 1
},
"user": {
"id": 12
}
}
So in order to get this i have put my code as this
let parameters :[String:AnyObject] = [
"warranty":product.warrenty,
"descriptions":product.longDescription,
"product_name":product.initialName,
"purchase_date":product.purchaseDate,
"serial_number":product.serialCode,
"product": [
"id":product.id
],
"user": [
"id":userDefaults.getCustomerId()
]
]
But i when i do the request it seems that server doesn't accept this format , probably the way i assign
"product": {
"id": 1
},
"user": {
"id": 12
}
is not correct , what is the issue in here ? can some one point me the issue that i do here
I had the same issue and the problem was, i was missing the encoding option which you need to set .JSON. Match the below line with yours.
Alamofire.request(.POST, strURL, parameters: parameter as? [String : AnyObject], encoding: .JSON).responseJSON { (response: Response<AnyObject, NSError>) in

Resources