Alamofire Invalid Value Around Character 0

Alamofire invalid value around character 0

I also faced same issue. I tried responseString instead of responseJSON and it worked. I guess this is a bug in Alamofire with using it with django.

Swift - Alamofire Invalid value around character 0.

Ok, finally I solved the problem

Here's new request :

Alamofire.request(url!, method: .post,encoding : JSONEncoding.default, headers: headers, parameters: params).responseJSON { (response) in
print(response)
}

With Headers :

  let headers = [
"Content-Type": "application/json"
]

Alamofire .post api error: Code=3840 Invalid value around character 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)



Related Topics



Leave a reply



Submit