I use the following code:
let params: Parameters = ["platform": "1"]
Alamofire.request(bannerURL, method: .post, parameters: params, encoding: URLEncoding.default, headers: configHeaders())
.responseJSON { (response) in
print(response)
}
but,I get:
FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
I try to use 'postman' or 'AFNetworking' ,and get json. I want to know how to solve this problem?
Alamofire version:4.7
Xcode version:9.2
Swift version:4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Platform(s) running Alamofire:iOS
macOS version running Xcode:10.13.3
Most likely, you're receiving an error page from the server. It will probably tell you what the issue was. If I had to guess, you probably shouldn't be using a URLEncoding with a POST request. I suggest you look into how the server expects parameters and adjust accordingly.
We use our GitHub project for bug reports and feature requests. In the future, you should open questions like this on Stack Overflow and tag alamofire.
Cheers. 🍻
From our Contribution Guidelines
We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.
Error was resolved after adding encoding: JSONEncoding.default with Alamofire.
Alamofire.request(urlString, method: .post, parameters: parameters,encoding:
JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
print(response)
break
case .failure(let error):
print(error)
}
}
Still I am getting error while using of "encoding: JSONEncoding.default"
Alamofire.request(self.serviceURL, method: .post, parameters: self.parameters,encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
self.state = .finished
switch response.result {
case .success:
self.state = .finished
self.processData(response: response.result.value as! [String:Any])
break
case .failure(let error):
// Call Completion Handler
self.completionHand(error as NSError,false,"Something went wrong.Please check your network connection and retry again")
print(error)
Still I am getting error while using of "encoding: JSONEncoding.default"
Alamofire.request(self.serviceURL, method: .post, parameters: self.parameters,encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
self.state = .finished
switch response.result {
case .success:
self.state = .finished
self.processData(response: response.result.value as! [String:Any])
break
case .failure(let error):
// Call Completion Handler
self.completionHand(error as NSError,false,"Something went wrong.Please check your network connection and retry again")
print(error)
please go to below link for more clarification
https://stackoverflow.com/questions/32355850/alamofire-invalid-value-around-character-0
FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
i got same issue ..issue only with first time.. i solved it by calling two api call one by one.
Alamofire.request(url,method: .get,parameters: withParameter,encoding:JSONEncoding.default,headers: headers as? HTTPHeaders).responseJSON{(responseData) -> Void in
print("==================")
print(responseData)
print("==================")
}
Alamofire.request(url,method: .get,parameters: withParameter,encoding:JSONEncoding.default,headers: headers as? HTTPHeaders).responseJSON{(responseData) -> Void in
print("==================")
print(responseData)
print("==================")
}
Error was resolved after adding encoding: JSONEncoding.default with Alamofire.
Still I am getting error while using of "encoding: JSONEncoding.default"
Func postApiMethod()
{
let headersAuth = ["Authorization" : "Bearer (authrizeToken)",
"Content-Type": "application/json"]
let parameter = ["ListName": “”] as [String : Any]
Alamofire.request(url, method: .post, parameters: parameter,encoding: JSONEncoding.default, headers: headersAuth).responseJSON {
response in
print(response.request!)
print(parameter)
switch response.result {
case .success:
print(response)
if let response = response.value as? [String:Any] {
print(response)
if ((response["success"] as? NSNumber) == 1)
{
}
else
{
}
}
break
case .failure(let error):
print(error)
}
}
}
Most helpful comment
Error was resolved after adding encoding: JSONEncoding.default with Alamofire.