简体   繁体   中英

Alamofire request changes method name on its own

I am using the following code:

func readInfo()
{
    let customHeader : HTTPHeaders = [
                "X-AUTH-TOKEN" : accessToken
            ]
    let body : Parameters = [
                :
            ]
    Alamofire.SessionManager.default.session.configuration.timeoutIntervalForRequest = 1000
    Alamofire.request(requestAddress, method: .get, parameters: body , encoding: JSONEncoding.default, headers: customHeader).responseJSON { 

    response in

     //utility code           

    }
}

It works perfect when this runs for the first time but when this is run more than once (in say less than 30 seconds), my server gives the error: osweb.servlet.PageNotFound : Request method 'T' not supported

Also I get status code 405 in Alamofire response. This is unexpected since I was sending .get request. Why is this happening and how should I avoid it? I am unable to understand.

Also, note that this is not a server error because the requests work as expected when run on Postman.

Try for Alamofire

 var parameters = Parameters()
 parameters = [
 //Your Params
 ]

 Alamofire.SessionManager.default.session.configuration.timeoutIntervalForRequest = 1000
 Alamofire.request("\(url)", method: .get, parameters: parameters, encoding: JSONEncoding.default)
  .responseJSON {
   response in switch (response.result)
    {
       case .success(let data):
          // your code for success
       break

       case .failure(let error):
           print("Server_Error",error.localizedDescription)
        break
    }

There were no errors with the cache or timeout. The error was with encoding . I had to save it to URLEncoding.httpBody to make the request work as expected. I still don't understand why it worked once and not for the second time for a certain seconds. Strange case but yes this was the solution. Please leave a comment to help me and others understand why this may have happened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM