Request calling twice after 401 server error - Alamofire - ios

Every time the server encountered a 401 error Alamofire duplicated the request. I want to stop that, I only want it to request once when it encounters 401 ?
AF.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: header)
.response(completionHandler: { response in
switch response.result {
case .success:
case .failure:
}
})

Related

How to handle empty response using Alamofire and Combine?

I am making a post request, which has an empty response
AF.request(URL(string: "some url")!, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil)
.validate()
.publishDecodable(type: T.self)
.value()
.eraseToAnyPublisher()
where T is
struct EmptyResponse: Codable {}
and I am having this error "Response could not be serialized, input data was nil or zero length."
How do I handle a post request with an empty response using Alamofire and Combine?
This error occurs when your backend returns no data but does not return an appropriate HTTP response code (204 or 205). If this is expected behavior for your backend, you can add your response code to the list of acceptable empty response codes when you set up the publisher: .publishDecodable(T.self, emptyResponseCodes: [200]. This also requires T to either conform to Alamofire's EmptyResponse protocol, or for you to expect Alamofire's Empty type as the response.
Found answer somewhere else but it's useful here. Made empty object like that:
struct EmptyEntity: Codable, EmptyResponse {
static func emptyValue() -> EmptyEntity {
return EmptyEntity.init()
}
}
And return publisher like so:
-> AnyPublisher<EmptyEntity, AFError>
AF.request(UrlUtils.base_url, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil).responseJSON { (response:AFDataResponse<Any>) in
switch(response.result) {
case .success(_):
// this case handles http response code 200, so it will be a successful response
break
case .failure(_):
break
}
}

Incorrect response from Alamofire?

I am making a request to a server using Alamofire. Here is how i am doing it:
Alamofire.request(url, method: .post, parameters: [:] ,encoding: JSONEncoding.default).responseJSON { response in
print("response=\(response)")
print("Response=:\((response.response?.statusCode)!)")
switch response.result{
case .success :
let passList = AuthenticateSuccess(nibName: "AuthenticateSuccess", bundle: nil)
self.navigationController?.pushViewController(passList, animated: true)
print("connected")
case .failure(let error):
self.showAlertTost("", msg: "Authentication Failed. Authenticate again!", Controller: self)
}
}
This is what prints:
response=SUCCESS: {
message = "Access denied.";
}
Response=:401
connected
I want to know that if 401 is error why is success block being executed? Is failure case in Alamofire handled differently?
As the documentation says:
By default, Alamofire treats any completed request to be successful, regardless of the content of the response. Calling validate() before a response handler causes an error to be generated if the response had an unacceptable status code or MIME type.
E.g.
Alamofire.request(url, method: .post, encoding: JSONEncoding.default)
.validate()
.responseJSON { response in
...
}
With validate, non 2xx responses will now be treated as errors.
response.success depicts that the server has returned the response. Whereas 401 is something that is related to the REST response which your backend system generated. Hence add the check to response code after verifying that you have received the response to provide better information to the end-user.

How to access redirected URL response from .POST HTTP request

I'm trying to connect to a payment server, which is supposed to redirect me to another URL other than the one I have in my code.
So I used Alamofire (.POST request) to connect to the server but I always enter the error case for "too many HTTP redirects" code=1007
And I know that the server should return that, What I need is to print the URL the server tried to redirect me to in my xcode console.
I have tried to use:
print(response.response?.allHeaderFields)
But didn't help in the output, Couldn't access the redirected URL.
let urlString = "http://www.gazdev.tk/PHP_VPC_3Party_Order_DO.php"
Alamofire.request(urlString, method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
print(response.response)
break
case .failure(let error):
print(response.response)
print(error)
}
}
I get this as print statement:
load failed with error Error Domain=NSURLErrorDomain Code=-1007 "too many HTTP redirects"
I think response.response?.url is what you want,eg:
AF.request("https://httpbin.org/get").responseJSON { response in
debugPrint("redirect = \(response.response?.url)")
}

Alamofire - responseSerializationFailed

I really need help with this one. I'm trying to do POST request using Alamofire, but for some reason I'm always getting an error from the title. When I test in POSTMAN, I'm getting okay response. Here is screenshot from POSTMAN, just to get things clearer:
And this is how I'm calling this API in code:
let parameters: Parameters = [
"data": [
"action":"homeimages"
]
]
print("Params: \(parameters)")
Alamofire.request(Constants.API_URL_2, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON {
response in
print("Response: \(response)")
switch response.result {
case .success(let value):
print("Response: \(value)")
break
case .failure(let error):
print(error)
}
}
As far as I know responseserializationfailed error, mostly error with API itself however as you stated, you are getting response in POSTMAN please check below things :
URL(check small/caps well), parameters are correct(check caps and dictionary format as well)
Sometimes we don't need below (optional) parameter , delete this parameter and check
encoding: JSONEncoding.default

Setting content-type header to use JSON with Swift 3 + AlamoFire

The answers in Alamofire Swift 3.0 Extra parameter in call did not work for me.
Setting header to nil compiles but I need ["Content-Type", "application/json"]. Here I get an error of an extra parameter in th emethod
How do I take
manager.request(url, method: .get, parameters: parameters).responseJSON {
response in
fulfill(response)
}
}
and send JSON content-type?
The documentation shows
Automatic Validation
Automatically validates status code within 200..<300 range, and that the Content-Type header of the response matches the Accept header of the request, if one is provided.
Alamofire.request("https://httpbin.org/get").validate().responseJSON { response in
switch response.result {
case .success:
print("Validation Successful")
case .failure(let error):
print(error)
}
}
I'm using .responseJSON but I'm not getting JSON back. So I think I need to send the Content-Type header.
Try this, there is another method overload that allow pass a dictionary with headers
let request = Alamofire.request(requestUrl, method: .get, parameters: [:], encoding: URLEncoding.queryString, headers: ["Content-Type" :"application/json"]).responseData { (response) in
/***YOUR CODE***/
}
for post JSON data in request check this answer Using manager.request with POST
Hope this helps you

Resources