I would like to state that I've tried every single example and scanned the plethora of other questions exactly like mine to no avail, so I have NO CHOICE but to post.
Xcode: 8.3.1 (8E1000a)
Apple Swift version 3.1 (swiftlang-802.0.51 clang-802.0.41)
Target: x86_64-apple-macosx10.9
OSX 10.12.4
Please see the code:
Note: This is embedded in a function, the parameters "email" and "password" are strings and are available, I also tried setting static string values.
let url = URL(string: "http://website.what.com/api/v1/login")
let params: Parameters = ["email": email, "password":password]
// This Works
Alamofire.request(url!, method: .post, parameters: params)
.responseJSON { response in
switch response.result {
case .success:
print(response)
case .failure(let error):
print(error)
}
}
// This one returns: "Extra argument Method in call"
let parameters: Parameters = [
"foo": [1,2,3],
"bar": [
"baz": "qux"
]
]
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseJSON { response in
switch response.result {
case .success:
print(response)
case .failure(let error):
print(error)
}
}
The primary I am confident in posting again - the second example was taken straight from the Alamofire github README.
If I am missing something - as in incorrectly encoding a parameter (which I copied directly from Alamofire's github page) or something.
Alternately, if someone can provide me a working sample on Swift 3.1 which allows me to use JSON, Headers, and POST Method.
The first "working" method does not have headers, I am not sure if I can use that example with additional parameters or not?
Sorry for the trouble, very very fresh swift learner who is diving head-first into a complicated world.
Related
One of my apps no longer works due to JSON serialisation failing when using Alamofire.
'responseJSON(queue:dataPreprocessor:emptyResponseCodes:emptyRequestMethods:options:completionHandler:)'
is deprecated: responseJSON deprecated and will be removed in
Alamofire 6. Use responseDecodable instead.
For code with the following lines
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { response in.. }
When changing to
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:])
.responseDecodable { response in... }
Then I get the error
Generic parameter 'T' could not be inferred
So I add the following
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:])
.responseDecodable(of: ResponseType.self) { response in.. }
I get the error
Cannot find 'ResponseType' in scope
Does anyone have any suggestions?
Unlike .responseJSON which returns a dictionary or array .responseDecodable deserializes the JSON into structs or classes.
You have to create an appropriate model which conforms to Decodable, in your code it's represented by ResponseType(.self).
The associated value of the success case is the root struct of the model.
But deprecated (in a yellow warning) means the API is still operational.
Side note: A JSON dictionary is never [String: Any?] the value is always non-optional.
In Alamofire -> Documentation/Usage.md, I found the definition of the Decodable, so I try like this, then found working.
struct DecodableType: Decodable {
let url: String
}
AF.request(urlString).responseDecodable(of: DecodableType.self) { response in
switch response.result {
case .success(let dTypes):
print(dTypes.url)
case .failure(let error):
print(error)
}
}
We're having this weird issue. The "+" is removed at the DB once we sent our request.
But if I print the response.data, I can see that the "+" is there (using Alamofire 4.9.1)
What could be the possible reason why the "+" is removed?
Here's how I set the request and the headers:
For the sake of future readers, application/x-www-form-urlencoded requests must be “percent encoded”, replacing the + character with %2B. But there are actually lots of characters that might need encoding, so it is generally best to use the Alamofire request method with Parameters, which takes care of all of those details for you. E.g. in Alamofire 4.9.1:
let headers: HTTPHeaders = [
"Authorization": authorization
]
let parameters: Parameters = [
"grant_type": "password",
"username": username,
"password": password
]
Alamofire.request(url, method: .post, parameters: parameters, headers: headers).responseJSON { response in
switch response.result {
case .failure(let error): print(error)
case .success(let value): print(value)
}
}
Or in Alamofire 5 and later, one would use AF rather than Alamofire:
AF.request(url, method: .post, parameters: parameters, headers: headers).responseJSON { response in
switch response.result {
case .failure(let error): print(error)
case .success(let value): print(value)
}
}
See the Alamofire usage document: POST Request With URL-Encoded Parameters
If you really want to build the httpBody of the URLRequest yourself, rather than just percent-encoding only the + character, you might consider a more generalized percent-coding routine, as shown in JSON request sending empty data. The idea is the same as what Alamofire’s Parameters, namely build the httpBody from a dictionary.
Turns out, the "+" is not being escaped properly. So I had to replace "+" with "%2B"
let requestString = "grant_type=password&username=" + encryptedTokenUsernameBody + "&password=" + encryptedTokenPasswordBody
let encodedString = requestString.replacingOccurrences(of: "+", with: "%2B")
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: o.s.web.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.
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
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