-1

I want to use Alamofire.request.validate() to validate http status code and return error If It doesn't pass the rule. As you know, It default check if It is 2xx. However, I want to add also HTTP 500. I tried to do Alamofire.request.validate().validate(500) but It doesn't work. In addition, because It accept sequence only, I can't use || operator. How can I achieve this? Thank you.

I got help from below post but can't find anything about my problem.

What is the use of the validate() method in Alamofire.request?

Emre Önder
  • 2,408
  • 2
  • 23
  • 73
  • `Alamofire.request.validate().validate(500)` is this a typo or you actually called validation twice? – Desdenova Dec 11 '18 at 11:31
  • Yes I called it twice. I find it somewhere on GitHub. However, actually, It is a wrong one. Btw, I don't know why this question gets down vote. – Emre Önder Dec 11 '18 at 11:32
  • `var codes = Array(200..<300); codes.append(500); Alamofire.request.validate(statusCode: codes)`? (Note: not tested), or in one line `Alamofire.request.validate(statusCode: Array(200..<300) + [500])` – Larme Dec 11 '18 at 11:34
  • Sorry. It doesn't accept array and sequence doesn't have a append option. – Emre Önder Dec 11 '18 at 11:36

1 Answers1

0

I can make Alamofire validate only HTTP 200 and HTTP 500 status codes by putting status codes inside an array and give as an input. Hope It helps:

Alamofire.request(route).validate(statusCode: [200,500]).responseJSON
Emre Önder
  • 2,408
  • 2
  • 23
  • 73