call web service with alamofire Swift - ios

How to call this kind of Web Service with alamofire.
http://www.example.com?abcrequest={"abc":"abc"}
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.responseSwiftyJSON({ (request, response, json, error) in
println(json)
println(error)
})
So what should I passed in URL and in parameter?

Related

when i am trying to print a json result using alamofire with swift 4 i am getting an "Error Domain=kCFErrorDomainCFNetwork Code=303"

Alamofire.request("https://www.googleapis.com/youtube/v3/playlists",
method: .get, parameters: ["part": "snippet", "channelId":
"UCMztOaBEOOswwu0wHlchkeA", "key":
"AIzaSyBHzTMlp1FkiIQJxda5UgSunikzfnQWnwQ" ], encoding:
JSONEncoding.default, headers: nil).downloadProgress(queue:
DispatchQueue.global(qos: .utility)) { Progress in
print("progress: \(Progress.fractionCompleted)")
}
.validate { request ,response ,data in
return .success
}
.responseJSON { response in
print(response)
debugPrint(response)
}
when i am trying to print the response i am getting error "Domain=kCFErrorDomainCFNetwork Code=303
Change JSONEncoding.default to URLEncoding.default
These parameters are query parameters defined here, thus you should use URLEncoding instead of JSONEncoding

How to set method, header, parameter in Alamofire 4 and Swift 3

In past version of Alamofire, for send method,header and parameter I used to do like this:
Alamofire.request(.GET, URLRequest, headers:headers, parameters: parameters)
but version 4 and swift 3 is different.
How can I set method, send header & parameter?
The migration guide at Alamofire github explains this very well.
Take a look here:
// Alamofire 3
let parameters: [String: AnyObject] = ["foo": "bar"]
Alamofire.request(.GET, urlString, parameters: parameters, encoding: .JSON)
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
print("Bytes: \(bytesRead), Total Bytes: \(totalBytesRead), Total Bytes Expected: \(totalBytesExpectedToRead)")
}
.validate { request, response in
// Custom evaluation closure (no access to server data)
return .success
}
.responseJSON { response in
debugPrint(response)
}
// Alamofire 4
let parameters: Parameters = ["foo": "bar"]
Alamofire.request(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default)
.downloadProgress(queue: DispatchQueue.utility) { progress in
print("Progress: \(progress.fractionCompleted)")
}
.validate { request, response, data in
// Custom evaluation closure now includes data (allows you to parse data to dig out error messages if necessary)
return .success
}
.responseJSON { response in
debugPrint(response)
}
The migration guide explained well, but there are no headers in the example, just to avoid confusions, bellow I add the example of a GET request to add them.
Alamofire.request(URL, method: .get, parameters: parameters, headers: headers)
.validate { request, response, data in
return .success
}
.responseJSON { response in
switch response.result {
case .success:
// do something
break
case .failure(let error):
// handle error
break
}
}
I found this information in here so, go and check it there if you have question related with the headers in the request.

Making a request in Alamofire 4.0 with a custom response

Having difficulty making a request in Alamofire 4.0. Previously I would use:
Code Snippet :
alamoManager.request(.GET, url, parameters: parameters, encoding: .url, headers: nil).responseObject { (response: Response<MyCustomResponse, NSError>) in
}
Where alamoManager is the old Manager object (now renamed SessionManager). However I can't see anything in the docs about how to pass a custom response (Conforms to Mappable). Has anybody achieved this? Any pointers would be really appreciated!
I used to type Alamofire 4.0 request like this:
import Alamofire
...
let parameters : Parameters = ["task":"setUser"]
Alamofire.request(url, parameters: parameters)
.validate()
.responseJSON { response in
//print(parameters), print(response.request), print(response.response), print(response.result)
switch response.result {
case .success:
let statusCode = (response.response?.statusCode)!
print("HTTP code #apiGetUser: \(statusCode)")
// ...
case .failure(let error):
// ...
}
}

Instagram on iOS8

I want to display my instagram on iOS app. I'm using Alamofire and Swifty libraries for JSON with this tutorial http://myxcode.net/2015/07/12/getting-data-from-instagram-account/
However I'm getting error in this line:
Alamofire.request(.GET, url).responseJSON { (request, response, json, error) in
saying "Void expects 1 argument but 4 were used in closure body".
But if I use
Alamofire.request(.GET, url)
.responseJSON { response in
print(response)
}
It prints the result correctly.
If i use
Alamofire.request(.GET, url)
.responseJSON { response in
let data = response["data"].arrayValue as [JSON]?
that I got from https://github.com/Alamofire/Alamofire
I get error "Type 'Response' has no subscript members"
How can I use this?
As mentioned on the Almofire GitHub page you have to use following if you want to get 4 parameters directly
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.response { request, response, data, error in
print(request)
print(response)
print(data)
print(error)
}
You can't use 4 parameters with the responseJSON as it only has one parameter as you have written in the example
but you can get 4 value from the response like this
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
You can get everything from the response object

Alamofire uploading progress

I need to send a file with the parameters and tracking the progress of uploading. Method
Alamofire.request(.POST, "http://httpbin.org/post", parameters: parameters, encoding: .JSON)
don't track progress uploading. Method
Alamofire.upload(.POST, "http://httpbin.org/post", file: fileURL)
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
println(totalBytesWritten)
}
.responseJSON { (request, response, JSON, error) in
println(JSON)
}
is not able to set the parameters
is it possible to send a file with the parameters and tracking the progress of uploading?
You have to use .uploadProgress instead of .progress.
Use this way
activeVidoeCell.uploadRequest = Alamofire.upload(fileData as Data, to: url, method: .put, headers: nil).uploadProgress(closure: { (progress) in
print(progress.fractionCompleted)
activeVidoeCell.downloadButton.setProgress(CGFloat(progress.fractionCompleted), animated: true)
}).responseJSON(completionHandler: { (result) in
completionHandler(result)
})

Resources