Alamofire request issue while using header - ios

I am trying to make post request and also sending header token but it is alway giving me error. Error is "Extra argument 'method' in call". I tried many ways to fix this but not succeed.
func eventsDate(){
let postData = ["month":12,
"year": 2017]
let headerToken = defaultObject.object(forKey: KHeaderToken)
let headers = ["Authorization": headerToken]
Alamofire.request(KCalendarUrl, method: .post, parameters: postData, encoding: JSONEncoding.default, headers: headers).responseJSON(completionHandler: { response in
print(response)
//to get status code
if let status = response.response?.statusCode {
switch(status){
case 200:
print("example success")
if let result = response.result.value {
let JSON = result as! NSDictionary
}
default:
print("error with response status: \(status)")
}
}
//to get JSON return value
})
}

The headers are supposed to be of type HTTPHeaders which in turn is [String:String].
The only issue that i can think of which is happening here is that headerToken is not of String type.
Use
let headerToken = defaultObject.object(forKey: KHeaderToken) as! String

You need to make sure you are conforming to the types required by the request function. In your case, you should make sure:
KCalendarUrl is URLConvertible => It can be either a String / URL / URLComponents / URLRequest (unless you use extensions / custom types conforming to URLConvertible)
headerToken is a String (headers must be a Dictionary of both String keys and values)

Related

Alamofire - Bad request

I am afraid I cannot share the API url. But I have checked on Postman, it works. It is a POST request and following is the response :
{
"user_key": "b0aebdedb15e2beaaf479ca3c4f8227e8e970681"
}
Postman screenshot :
In code, this is the request I am making using Alamofire :
Alamofire.request("some url", method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: ["Content-Type":"application/json"])
.responseObject { (response: DataResponse<User>) in
let userResponse = response.result.value
print("")
}
But userResponse comes to be nil. This is the User object :
import ObjectMapper
class User: Mappable {
var authToken: String?
required init?(map: Map) {
}
func mapping(map: Map) {
self.authToken <- map["user_key"]
}
}
I am using ObjectMapper here.
Assuming the it is not a JSON object, I tried to handle it as a String :
Alamofire.request("some url", method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: ["Content-Type":"application/json"])
.responseString { (response: DataResponse<String>) in
let dataUser = response.data as! Any
let dataUser1 = response.data as! AnyObject
let error = response.error
let code = response.response?.statusCode
print(response.value)
print(response.result.value)
}
Still, nothing. I am not able to retrieve the value. The status code however comes out to be 400 (bad request).
What exactly is it ? JSON object or String ? How do I fix this ?
Just try to replace
this URLEncoding.httpBody to JSONEncoding.default
JSONEncoding.default
Uses JSONSerialization to create a JSON representation of the parameters object, which is set as the body of the request. The Content-Type HTTP header field of an encoded request is set to application/json.
Alamofire reference

Error while parsing JSON data received via Alamofire (Swift)

I'm working with a non-profit organisation to help them develop a mobile application for their website (so they provided me with the backend services, etc)
Their login PHP service accepts data in JSON, and returns a true or false value in the form of JSON data, but I'm having trouble processing the response in Swift. I'm using Alamofire to connect to the HTTP service.
I'm attaching my code and the exception message I'm receiving below, would really appreciate some help
func authenticateUser (un: String, pw: String) -> Bool
{
var checker = false
let jsonDict : [String: String] = ["volunteer_email": un, "volunteer_pass": pw]
Alamofire.request("www.sampleurl.com/login.php", method: .post, parameters: jsonDict, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if response.result.value != nil{
print (response.result.value)
let resp = response.result.value as! NSDictionary
let results = resp["status"] as! [[String:Any]]
//Change the boolean value of checker based on the value of the results constant
print (results)
}
break
case .failure(_):
print(response.result.error)
break
}
}
return checker;
}
Log:
Optional(<__NSSingleObjectArrayI 0x600000009b50>( {
status = false; } ) ) Could not cast value of type '__NSSingleObjectArrayI' (0x10e28c528) to 'NSDictionary'
(0x10e28d1a8). 2018-05-09 12:13:00.177091+0530 TestApp1 [16680:817883]
Could not cast value of type '__NSSingleObjectArrayI' (0x10e28c528) to
'NSDictionary' (0x10e28d1a8). (lldb)
Log for response.result.value:
Note: "status":"false"/"true" is the output from the web service
Optional(<__NSSingleObjectArrayI 0x60400001b160>(
{
status = false;
}
))
Note: I did some research and I understood what the NSSingleObjectArray is and what causes it, but I'm stuck here as the service passes back only a single JSON value to my app. Is there any way to handle this without requesting the organisation to change their code? Logically, shouldn't I be able to cast the response into an NSDictionary regardless of its size?
Also, the reason why I've specified that the returned data can be of type any, is because I ran into an issue that can be found here:
Other StackOverflow question
Thanks so much in advance :)
Since you're using .responseJSON of Alamofire, you should make use of Alamofire's parameterized enums. It provides the json object in .success. So doing case .success(_) is wasteful.
Go ahead with this and no need of typecasting response.result.value at all.
Alamofire
.request("www.sampleurl.com/login.php",
method: .post,
parameters: jsonDict,
encoding: JSONEncoding.default,
headers: nil)
.responseJSON { (response) in
switch(response.result) {
case .success(let responseJSON):
print(responseJSON)
/*
As an improvement:
To obtain an easy-access object, convert responseJSON to either a:
1. Codable model:
let model = JSONDecoder().decode(SomeModel.self,
from: responseJSONData)
2. SwiftyJSON object: (available on GitHub)
let json = JSON(responseJSON)
Doing so will basically make accessing the inner elements easier.
*/...
case .failure(let error):
print(error)
}
}
BTW, just FYI: the response is an array of dictionaries, not a dictionary of array of dictionaries.
Use NSArray instead of NSDictionary
func authenticateUser (un: String, pw: String) -> Bool{
var checker = false
let jsonDict : [String: String] = ["volunteer_email": un, "volunteer_pass": pw]
Alamofire.request("www.sampleurl.com/login.php", method: .post, parameters: jsonDict, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if response.result.value != nil{
print (response.result.value)
let resp = response.result.value as! NSArray
let results = resp["status"] as! [[String:Any]]
//Change the boolean value of checker based on the value of the results constant
print (results)
}
break
case .failure(_):
print(response.result.error)
break
}
}
return checker;
}

Replicating post method in Postman with parameters and body-raw into Alamofire

I'm having a problem on using Alamofire. When I try to post a request using a generic parameters like ["name":"John", "age":"27"] it always succeeds. But, when I try to use a web service that requires parameters and a body-raw for a base64 string I'm not able to get a successful response from the server. Though it succeeds when I use Postman. Does anyone knows how to do this on Alamofire 4? Here is the screenshot of my postman.
Thank you!
#nathan- this is the code that I used. I just assumed that the base64String inside the "let paramsDict" has a key value named "data" though it doesn't have a key name in postman.
let urlString = ApiManager.sharedInstance.formsURL + ApiManager.sharedInstance.mobileFormsImageUpload
let paramsDict = ["token": token, "fileID":"2", "filename":"images.png", "data": base64String] as [String : Any]
Alamofire.request(urlString, method: .post, parameters: paramsDict, encoding: URLEncoding.httpBody, headers: [:])
.responseJSON{ response in
switch response.result {
case .success(let data):
debugPrint("SUCCESS")
case .failure(let error):
debugPrint("Request Error")
}
}
I already figured it out. It needs a custom encoding to make it work. All the parameters must be inlined with the url so the base64 string inside the parameter is the only to be encoded. Here is the code that I used.
struct CustomPostEncoding: ParameterEncoding {
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try URLEncoding().encode(urlRequest, with: parameters)
let base64 = parameters?["data"] as! String
let finalBase64Format = "\"" + base64 + "\""
let postData = NSData(data: finalBase64Format.data(using: String.Encoding.utf8)!)
request.httpBody = postData as Data
return request
}
}
func uploadImageBase64(){
let jpegCompressionQuality: CGFloat = 0.9 // Set this to whatever suits your purpose
if let base64String = UIImageJPEGRepresentation(testIMG, jpegCompressionQuality)?.base64EncodedString() {
var token = String()
if let data = UserDefaults.standard.data(forKey: "userProfile"),
let user = NSKeyedUnarchiver.unarchiveObject(with: data) as? UserProfile{
token = user.token
} else {
print("There is an issue")
}
let headers = [
"content-Type": "application/json"
]
let urlString = "http://localhost/FormsService.svc/Getbase64?filename=test.png&fileID=1151&token=80977580xxx"
let paramsDict = ["data": base64String] as [String : Any]
Alamofire.request(urlString, method: .post, parameters: paramsDict, encoding: CustomPostEncoding(), headers: headers)
.responseJSON{ response in
print("response JSON \(response.result)")
}
.response{ response in
print("RESPONSE \(response)")
}
}
}

Receiving MailChimp error "Your request did not include an API key." even though the API key is included using Alamofire

I am using Alamofire to send a request to MailChimp to add a user to a list
MailChimp's docs say:
There are 2 authentication methods for the API: HTTP Basic authentication and OAuth2. The easiest way to authenticate is using HTTP Basic authentication. Enter any string as your username and supply your API Key as the password.
The request I wrote for Alamofire:
let params: [String : AnyObject] = ["email_address": email, "status": "subscribed", "merge_fields": [ "FNAME": name]]
guard let url = "https://us10.api.mailchimp.com/3.0/lists/<listID>/members/".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) else { return }
Alamofire.request(.POST, url, parameters: params, encoding: .URL)
.authenticate(user: "apiKey", password: "<apikey>")
.responseJSON { response in
if response.result.isFailure {
}
else if let responseJSON = response.result.value as? [String: AnyObject] {
}
}
I checked that the API key is correct by using it to access their playground:
https://us1.api.mailchimp.com/playground/
The response I get back states that the API key was not included:
Your request did not include an API key.
Where have I gone wrong?
Swift 3
Make sure you take a look at MailChimp's Error Glossary. A 401 indicates that your API key is not being read properly.
For Swift 3, the header construction in Abbey Jackson's answer needs to be updated to this. Otherwise it totally works.
let credentialData = "AnyString:\(apiKey)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString()
let headers = ["Authorization": "Basic \(base64Credentials)"]
Here's an example that uses Request.authorizationHeader instead.
let apiKey: String = "xxxxxxxxxxxxxxx2b-us11" // note the 'us11'
let baseUrl: String = "https://us11.api.mailchimp.com/3.0" // note the 'us11'
let listId: String = "xxxxxx2f"
func createListMember() {
let url = "\(baseUrl)/lists/\(listId)/members"
guard let authorizationHeader = Request.authorizationHeader(user: "AnyString", password: apiKey) else {
print("!authorizationHeader")
return
}
let headers: HTTPHeaders = [
authorizationHeader.key: authorizationHeader.value
]
let parameters: Parameters = [
"email_address": email,
"status": "subscribed"
]
// perform request (make sure you're using JSONEncoding.default)
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
//.authenticate(user: "AnyString", password: apiKey) // this doesn't work
.validate()
.responseJSON {(response) in
print(response)
}
}
So MailChimp actually needs the api key sent in the authorization header like this:
let params: [String: AnyObject] = ["email_address": email, "status": "subscribed"]
guard let url = "https://us10.api.mailchimp.com/3.0/lists/<listID>/members/".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) else { return }
let credentialData = "user:<apikey>".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(.POST, url, headers: headers, parameters: params, encoding: .URL)
.responseJSON { response in
if response.result.isFailure {
}
else if let responseJSON = response.result.value as? [String: AnyObject] {
}
}
edit: See Derek Soike's answer below for Swift 3
If you are passing the apikey as like bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5dxf-us10, then you'll get the error as you specified.
Try to pass the apikey by skipping the characters after the hyphen (like "bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5dxf") and check if it works.
Swift 3 As of Sept. 5, 2017
It says method is an extra argument by accident. It's likely that your encoding part is off. It should look like this:
encoding: JSONEncoding.default

Alamofire NSURLRequest via POST method doesn't work

I'm new to swift and iOS and trying to use Alamofire and router for them, which returns NSMutableURLRequest, but my code didn't work. So I just made one NSURLRequest for test, and requested it but results was same.
Here is my code. I'm currently using Alamofire and SwiftyJSON.
let params = ["Id": "1234567", "Token": "something"]
let url = NSURL(string: "myurl")
var request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = Alamofire.Method.POST.rawValue
let encoding = Alamofire.ParameterEncoding.JSON
(request, _) = encoding.encode(request, parameters: params)
Alamofire.request(request)
.validate()
.responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
let token = json["token"].stringValue
let error = json["error"].stringValue
print("token : \(token), error : \(error)")
}
case .Failure(let error):
// TODO:
print(error)
}
}
Above code sends request without parameters. Are there any errors on my code?
I've checked your code and before executing encode function your request.HTTPBody is empty, but after it has some data like
Optional<NSData>
- Some:<7b22546f 6b656e22 3a22736f 6d657468 696e6722 2c224964 223a2231 32333435 3637227d>
When I call print(response.request?.HTTPBody) in Alamofire response block, I get the parameters as NSData and the HTTPBody includes the same data as before sending the request so it works.
Try also change the response from responseJSON to responseString, because if your response can't be parsed to JSON you get Failure.
I think you should check on your URL site if you get correct data.
Instead of your solution I use
Alamofire.request(method, url, parameters: parameters, encoding: .JSON) .responseString{ response in}
is the same but shorter and everything is as parameters.

Resources