-1

I am trying to make a Alamofire POST request. This is how I am making the request..

     Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers : headers)
        .responseString { response in
            print(response.result)
 }

Though I am getting the result as 'SUCCESS', the status code is always shown as 405 while it should have been 200. In the 'encoding' part of the request, I have tried everything like JSONEncoding.default,JSONEncoding.prettyPrinted, URLEncoding.httpbody...but always the status code is still 405. Can anyone please help? Thanks in advance...

D.M
  • 490
  • 6
  • 14

4 Answers4

2

This is the solution for this issue...A couple of changes had to be made..

The header which was given was this: let headers = [ "Content-Type" : "application/json"]. But it had to be let headers = [ "Content-Type":"application/x-www-form-urlencoded"]. Also the encoding should be given as URLEncoding.httpBody.

Making these changes made it work fine...

D.M
  • 490
  • 6
  • 14
1

I think problem is with your server because this status code only comes when server disable the api

The HTTP 405 Method Not Allowed response status code indicates that the request method is known by the server but has been disabled and cannot be used. The two mandatory methods, GET and HEAD, must never be disabled and should not return this error code.

So Contact your server(backend developer), make sure your url is correct

Jaydeep Vyas
  • 4,068
  • 1
  • 15
  • 41
1

I have faced same problem. The API endpoint worked fine with Android Retrofit, also tested with PostMan. Also the header's Content-Type was application/json. It was really strange bug. I've used Fiddler to check the response. The error message was

The requested resource does not support http method 'T'/'ST'.

I used GET/POST method, but it said I was using the T/ST, instead of GET/POST

I found answer from the Alamofire's issues.

When I called the API endpoint without parameters, I used the blank dictionary as parameter.

Alamofire.request("URL", method: .get, parameters: [:], encoding: JSONEncoding.default).responseString(completionHandler: completionHandler)

I changed it to nil

Alamofire.request("URL", method: .get, parameters: nil, encoding: JSONEncoding.default).responseString(completionHandler: completionHandler)

After that, it worked fine. Hope it will help you. Cheers!

Green Y.
  • 193
  • 1
  • 10
-1

Try to replace:

responseString with responseJSON AND URLEncoding(destination: .queryString) with URLEncoding.default

LIKE:

Alamofire.request(strURL, method: .post, parameters: params, encoding: URLEncoding.default, headers: headers).responseJSON { (responseObject) -> Void in 

//Do something

}
Anurag Sharma
  • 2,823
  • 1
  • 21
  • 38