status code 405 issue..Alamofire POST request... - ios

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...

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...

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

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!

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
}

Related

Network Connection Lost

I am facing to
Error - The network connection was lost.
I am using RealmSwift and Alamofire in my project.
This is a real estate app and when ever I am searching properties suddenly I am facing to this error without any exception or error and if I searching again than it works properly.
I don't understand why I am facing this issue.
I am using following code for JSON encoding:
Alamofire
.request(url, method: method, parameters: parameters, encoding: encoding, headers: header)
.responseJSON(completionHandler: { (response) in
Please help.
Are you encoding the parameters as JSON in the request body? If so, encode them as URL:
Alamofire.request(.GET, myURLString, parameters: parameters, encoding: .URL, headers: myHeaders)

How to send `apikey` in header in Alamofire 4.5,Swift 4?

I want to make a HTTP post request via Alamofire 4.5. The request need an authorization header(which is a Api key). But whenever I fired the request,my server cant detect the ApiKey.'
Here is how I make the Alamofire request
let params : [String : Any] =["param1":param1,"param2":param2]
let headers : HTTPHeaders = ["authorization" : apiKey]
Alamofire.request(MY_URL, method: .post, parameters: params, headers: headers).responseJSON {
response in
switch response.result{
case .success(let result):
//other code here
}
I triple checked the value of apiKey ,the value is correct,but the request sent,my server cant detect the authorization at all.
I totally no idea whether I do anything wrong here,cause I very new in Swift.Kindly provide a proper solution.Thanks
Edit :
In my server code,I using Slim 2
$app->map('/MY_URL','authenticate',function ()use($app){
}
'authenticate' is the point that scan for the authorization: apiKey in the headers,so now the problem is my server cant get the value of apiKey therefore always giving the same error "Api Key is missing" which I set when no Api Key found.
I tried the method below in Alamofire Documentation,but the result still the same.
What I tried:
let headers: HTTPHeaders = [
"Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
"Accept": "application/json"
]
Alamofire.request("https://httpbin.org/headers", headers: headers).responseJSON { response in
debugPrint(response)
}
What I missing here?Somebody please give me some hints to do it..Thank you.
EDIT:
To be more clear on my I mean for authorization : apiKey I show the way I make request in Postman.
Normally I just insert the "authorization": apiKey in the Headers in the request
but in Swift,the web service cant get the value of apiKey,therefore the server always return this following response :
{
"error": true,
"message": "Api key is missing"
}
This is working fine for me with Alamofire 4.6.0
let url = "WEB API URL"
let headers = [
"Content-Type":"application/x-www-form-urlencoded",
"authorization" : "apiKey"
]
let configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
let params : [String : Any] = ["param1":param1,"param2":param2]
Alamofire.request(url, method: .post, parameters: params as? Parameters, encoding: URLEncoding.httpBody, headers: headers).responseJSON { response in
if let JSON = response.result.value {
print("JSON: \(JSON)")
}else{
print("Request failed with error: ",response.result.error ?? "Description not available :(")
}
}
TLDR;
The problem is that iOS's URLRequest automatically capitalize headers. At the same time you API does not follow best practices.
Change your API to comply to RFC 7230 and allow it to accept headers case-insensitively.
The whole story:
At first, your question seemed a bit odd since there is no obviously wrong code in what you provided. Nevertheless I tried to reproduce your request in Postman.
Now we should stop and I must warn you to never post what you did in your "Here is my request" section. The information given there allowed me to completely reproduce your request in Postman (including headers and exact fields' names and values), which is good to solve your problem. But at the same time you shared your presumably private and maybe even bought API key to everyone who see your question. Which is obviously not good and I would recommend you to change your API key if it is possible.
Then I tried your code and noticed exactly the same behavior you talking about. I debugged responseJSON closure and observed response.request?.allHTTPHeaderFields property:
(lldb) po response.request?.allHTTPHeaderFields
▿ Optional<Dictionary<String, String>>
▿ some : 2 elements
▿ 0 : 2 elements
- key : "Content-Type"
- value : "application/x-www-form-urlencoded; charset=utf-8"
▿ 1 : 2 elements
- key : "Authorization"
- value : "f8f99f9506d14f0590863d5883aaac9b"
(if you don't understand what I wrote read about debugging in xcode and in particular for lldb's po command)
As you can see, authorization header's name start with a capital A letter even though I passed it all lowercased.
I tried to send new request with postman with capital A and yes - I learned that your API accepts only lower-cased authorization header name.
"It isn't really a problem" you think right now. "We should just change our authorization header name somewhere and it should be just fine, right?"
NOT SO EASY.
I tried a few things which all lead me to the URLRequest's setValue(_:forHTTPHeaderField:) method. Alamofire calls it and I tried it too. Surprisingly enough after calling this method "authorization" header always changes to "Authorization". Then I found the thing that particularly interesting for us:
Note that, in keeping with the HTTP RFC, HTTP header field names are case-insensitive.
Keep in mind that I even tried to change URLRequest's allHTTPHeaderFields directly. Had the same result.
Which leads us to the following conclusion: Apple intentionally ignores input headers' case and very irresponsibly changes it (again intentionally since it takes at least a few lines of code somewhere instead of just plugging given headers directly into request). As of now I know no possible solution to this problem (if we want to classify it as a problem which is a bit controversial). Search says that is exists from earlier days of iOS (http://0xced.blogspot.com.by/2010/06/fixing-nsmutableurlrequest.html). You could call some private objective-c APIs which could help, but in fact you'll get unstable or undefined behavior and would likely get rejected from App Store.
So my conclusion, and probably the only right choice in this situation is to change your API.
Configuration is optional, the only thing you need is to setup request right. Make sure (double sure) that you format your auth correctly.
In some (not that rare cases this should be formatted like this:
["Authorization": "Bearer <#your_token#>"]
And what I found about Slim 2 it's also with Bearer so maybe you missing this.
https://github.com/dyorg/slim-token-authentication/tree/master/example#making-authentication-via-header
Example from this:
$ curl -i http://localhost/slim-token-authentication/example/restrict -H "Authorization: Bearer usertokensecret"
With this, you can also check if this working with simple curl command. It should. If not, there is definitely a problem with fields you're sending rather than Alamofire itself.
In docs for Alamofire you can find:
/// Creates a `DataRequest` using the default `SessionManager` to retrieve the contents of the specified `url`,
/// `method`, `parameters`, `encoding` and `headers`.
///
/// - parameter url: The URL.
/// - parameter method: The HTTP method. `.get` by default.
/// - parameter parameters: The parameters. `nil` by default.
/// - parameter encoding: The parameter encoding. `URLEncoding.default` by default.
/// - parameter headers: The HTTP headers. `nil` by default.
///
/// - returns: The created `DataRequest`.
public func request(_ url: URLConvertible, method: Alamofire.HTTPMethod = default, parameters: Parameters? = default, encoding: ParameterEncoding = default, headers: HTTPHeaders? = default) -> Alamofire.DataRequest
Here is an example:
Alamofire.request("https://...",
method: .get,
parameters: ["myKey1": "myValue1"],
encoding: JSONEncoding.default,
headers: self.authHeader).responseJSON { response in
//your response
}

Alamofire 4.0 - Set request content type

I'm using Alamofire 4.4.0.
I need to send POST request with params in url string (as in GET request) and with content type "application/x-www-form-urlencoded".
My code is following:
Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default)
.validate()
.responseJSON
Issue is that content type is always "application/json"
I also tried to set encoding to URLEncoding.queryString or .httpBody, also tried to set headers for request as headers: ["Content-Type": "application/x-www-form-urlencoded"]
Please give some advice, how to set request content type in Alamofire 4.0

Fetching http request with query in Alamofire in Swift

I'm trying to fetch data from mLab service from my mongodb database. I can make the request successfully from browser and get data with code below.
https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={"member_id":2}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI
I need to change "member_id" to \"member_id\" to not to get sytax error. The rest of is same. However, It doesn't fetch anything with Alamofire in Swift in iOS. (I also tried it without alamofire, with usual http request but still doesn't work)
If I try it without {"member_id":2} It is working.
I'm doing fetching with below code (not working one);
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={\"member_id\":2}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI")
I also try to add parameters
let parameters: Parameters = ["member_id": "3"]
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={\"member_id\":3}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI", parameters: parameters, encoding: URLEncoding(destination: .methodDependent))
This is the api document;
http://docs.mlab.com/data-api/
Thank You
Your request should look like that:
let parameters: Parameters = [
"q": ["member_id": 2],
"apiKey": "2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI"
]
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp", method: .get, parameters: parameters, encoding: URLEncoding.default)
It is more readable, and also you can easily change the parameters.
Hope it helps

Alamofire 4 network stream closed

I am trying to make a POST request using latest Alamofire 4.0.0 / Swift 3 passing JSON in the request.
The request calls a wildfly server and takes up to a minute to return with data.
However, I constantly get a server error java.io.IOException: UT010029: Stream is closed.
let headers: HTTPHeaders = [
"Content-Type": "application/json",
"Accept": "application/json",
"Connection": "Keep-Alive"
]
Alamofire.request("myUrl", method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers)
.validate().responseJSON { response in
print("Response")
print(response)
}
}
Alamofire doesn't complain at all and doesn't log anything.
I have tried other requests to the same server/service that return instantly and I get into the completion handler.
Is this just a timeout issue? If so how can I adjust it?
I was getting a very similar problem with GET requests. Turns out I was passing an empty dictionary [:] as parameters to the request method. Omitting the parameters: completely in the call solved the issue.

Resources