Alamofire POST request with Swift 2 - json

I am trying to make a POST request in Alamofire to return a JSON object. This code worked in Swift 1, but in swift 2 I get this invalid parameter issue:
Tuple types '(NSURLRequest?, NSHTTPURLResponse?, Result<AnyObject>)' (aka '(Optional<NSURLRequest>, Optional<NSHTTPURLResponse>, Result<AnyObject>)') and '(_, _, _, _)' have a different number of elements (3 vs. 4)
It seems like the error parameter was removed, but I am using the error parameter inside the function to check for errors, so how would I do that without an error param?
Here's my code for the POST request:
let response = Alamofire.request(.POST, urlPath, parameters: parameters, encoding: .URL)
.responseJSON { (request, response, data, error) in
if let anError = error
{
// got an error in getting the data, need to handle it
print("error calling POST on /posts")
print(error)
}
else if let data: AnyObject = data
{
// handle the results as JSON, without a bunch of nested if loops
let post = JSON(data)
// to make sure it posted, print the results
print("The post is: " + post.description)
}
}

If you see the documentation in the branch Swift2.0 you can see that the responseJSON function has changed as the error says, it have now three parameters but you can catch the error too, lets take a look:
public func responseJSON(
options options: NSJSONReadingOptions = .AllowFragments,
completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<AnyObject>) -> Void)
-> Self
{
return response(
responseSerializer: Request.JSONResponseSerializer(options: options),
completionHandler: completionHandler
)
}
Now it returns an enum Result<AnyObject> and according to the doc :
Used to represent whether a request was successful or encountered an error.
- Success: The request and all post processing operations were successful resulting in the serialization of the
provided associated value.
- Failure: The request encountered an error resulting in a failure. The associated values are the original data
provided by the server as well as the error that caused the failure.
And it have inside an property entitled error, with the following doc:
/// Returns the associated error value if the result is a failure, `nil` otherwise.
public var error: ErrorType? {
switch self {
case .Success:
return nil
case .Failure(_, let error):
return error
}
}
Then if you follow this test case inside Alamofire you can see how to get the error properly:
func testThatResponseJSONReturnsSuccessResultWithValidJSON() {
// Given
let URLString = "https://httpbin.org/get"
let expectation = expectationWithDescription("request should succeed")
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var result: Result<AnyObject>!
// When
Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
.responseJSON { responseRequest, responseResponse, responseResult in
request = responseRequest
response = responseResponse
result = responseResult
expectation.fulfill()
}
waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
// Then
XCTAssertNotNil(request, "request should not be nil")
XCTAssertNotNil(response, "response should not be nil")
XCTAssertTrue(result.isSuccess, "result should be success")
XCTAssertNotNil(result.value, "result value should not be nil")
XCTAssertNil(result.data, "result data should be nil")
XCTAssertTrue(result.error == nil, "result error should be nil")
}
UPDATE:
Alamofire 3.0.0 introduces a Response struct. All response serializers (with the exception of response) return a generic Response struct.
public struct Response<Value, Error: ErrorType> {
/// The URL request sent to the server.
public let request: NSURLRequest?
/// The server's response to the URL request.
public let response: NSHTTPURLResponse?
/// The data returned by the server.
public let data: NSData?
/// The result of response serialization.
public let result: Result<Value, Error>
}
So you can call it like the following way:
Alamofire.request(.GET, "http://httpbin.org/get")
.responseJSON { response in
debugPrint(response)
}
You can read more about the migration process in the Alamofire 3.0 Migration Guide.
I hope this help you.

Have you tried using three parameters in the .responseJSON and inserting a try catch block around the areas you want error logging, check out this link if you need help with swift 2 try catchs

Related

Parse JSON when I get a error response in Alamofire

I've been working on API requests using Alamofire, and I want to know something for parsing the Error and to get the JSON data from the server instead of accessing the AFErrors. The code below works fine, but since I've been using .responseDecodable for decoding the response and also the MyError inherits Decodable, I was wondering if there is a similar way to decode data when I get the error response back, instead of using JSONDecoder().decode.
When I get the error response, it returns a JSON object with one value in it (message) from the server.
static let sessionManager: Session = {
let configuration = URLSessionConfiguration.af.default
configuration.timeoutIntervalForRequest = 30
return Session(configuration: configuration)
}()
Request.sessionManager.request(endpoint, method: httpMethod, parameters: params, headers: headers)
.responseDecodable(of: resDecodeType.self) { response in
switch response.result {
case .success(let successResponse):
completed(.success(successResponse))
case .failure(let error):
let errorStatusCode = response.response?.statusCode
do {
let data = try JSONDecoder().decode(MyError.self, from: response.data!) // -> this part...
} catch {
print(error)
}
}
}
}
struct MyError: Decodable {
let message: String
}

SwiftyJSON - Unable to get transactionId from JSON String

I am using CocoaAsyncSocket to retrieve a message from a servers API that's using JSON. I am able to get the data and convert it to a printable string, what I am unable to do is retrieve a value (transactionId) from my attempts to parse the JSON string I already have, using SwiftyJSON. I know there are other posts that are similar to this one but none have solved my problem.
In ViewController:
func socket(_ sock: GCDAsyncSocket, didRead data: Data, withTag tag: Int) {
guard let msg = String(data: data, encoding: .utf8) else { return }
var response = " "
if msg.contains("Reset") {
transactionID = ParseJSON().parse(message: msg)
response = String(format: "{\"Response\":{\"transactionId\":\"%#%\",\"content\":{\"Reset\":{}}}}", transactionID)
socket.write((response.data(using: .utf8))!, withTimeout: -1, tag: 0)
}
socket?.readData(withTimeout: -1, tag: 0)
}
ParseJSON class:
func parse (message: String) -> String {
var parsedMessage = " "
let json = JSON(parseJSON: message)
let transactionId = json["Request"]["transactionId"].stringValue
parsedMessage = transactionId
print(parsedMessage)
return parsedMessage
}
The result that is displayed is an empty transactionId value. Nothing prints or anything.
If you spot any errors in my code or have a better approach then please let me know!
Edit:
Here is the string I am attempting to parse:
{"Request": {"content": {"Reset": {}}, "transactionId": "f7c4d630-552b-46d9-a37d-44450537b48d"}}
Here is my output:
{\"Response\":{\"transactionId\":\"\",\"content\":{\"Reset\":{}}}}
The problem is not the code. Consider:
func parse(message: String) -> String {
let json = JSON(parseJSON: message)
return json["Request"]["transactionId"].stringValue
}
let input = """
{"Request": {"content": {"Reset": {}}, "transactionId": "f7c4d630-552b-46d9-a37d-44450537b48d"}}
"""
let transactionID = parse(message: input)
print("transactionId:", transactionID)
let response = String(format: "{\"Response\":{\"transactionId\":\"%#\",\"content\":{\"Reset\":{}}}}", transactionID)
print("response:", response)
The result of the above, as you’d expect, is:
transactionId: f7c4d630-552b-46d9-a37d-44450537b48d
response: {"Response":{"transactionId":"f7c4d630-552b-46d9-a37d-44450537b48d","content":{"Reset":{}}}}
So I suspect that the input message is not quite what you expected. So I might suggest adding some error handling so you can diagnose precisely where it is going wrong:
func parse(message: String) -> String {
let json = JSON(parseJSON: message)
if let error = json.error {
fatalError("JSON parsing error: \(error)")
}
let request = json["Request"]
if let error = request.error {
fatalError("request error: \(error)")
}
let transactionId = request["transactionId"]
if let error = transactionId.error {
fatalError("transactionId error: \(error)")
}
return transactionId.stringValue
}
Now, in practice, you probably wouldn’t use fatalError, but rather would do some graceful error handling (e.g. change parse such that it throws, then throw the errors encountered, if any, and then catch the error where you call parse and handle any runtime issues gracefully). But during this diagnostic process, fatalError is useful because it will stop your debugger at the offending line, simplifying your diagnostic process.
Bottom line, the request must not be quite in the form you expect. Note, it’s going to be very sensitive to capitalization, malformed JSON, etc. So, by looking at the errors provided by SwiftyJSON, you should be able to narrow down the issue quite quickly.
Below, you tell us that the Data is:
<7b225265 71756573 74223a20 7b22636f 6e74656e 74223a20 7b225265 73657422
3a207b7d 7d2c2022 7472616e 73616374 696f6e49 64223a20 22643937 36303036
622d3130 38302d34 3864652d 39323232 2d623139 63363663 35303164 31227d7d
00>
The problem is that last byte, 0x00. If you remove that, it works.
FWIW, when I convert that hex string back to a Data and run it through JSONSerialization, it confirms the diagnosis:
Error Domain=NSCocoaErrorDomain
Code=3840 "Garbage at end."
UserInfo={NSDebugDescription=Garbage at end.}
You need to figure out why that 0x00 was included in the end of your payload and remove it.

How to parsing we parsing dictionary of array in swift by using object mapper

I facing problem with this how to overcome this.
func addressApi()
{
let params = ["":""]
WebService.shared.apiGet(url: addressApiURL, parameters: params) { (response, error) in
if error == nil
{
guard let data = response?["data"] else{return}
guard let address = data["address"] as? string else{return}
}
}
}
I getting response like this : Response
I want to getting address and locationName from that response how is it.
I facing error this: Error
Here we are don't getting the values superately from array of dictionaries.
For that one you need to write the for loop.
If incase you are using Modelclass then then directly load that class into collection view or tableview

Send POST request with JSON object and query param to REST webservice using alamofire

I'm trying to send POST request to REST webservice using alamofire
I'm passing json object as POST body, and i'm getting the response and everything works fine till now
Alamofire.request(.POST, path, parameters: createQueryParams(), encoding: .JSON)
.responseArray { (request, response, myWrapper, error) in
if let anError = error
{
completionHandler(nil, error)
println("Error in handling request or response!")
return
}
completionHandler(myWrapper, nil)
}
private class func createQueryParams() -> [String:AnyObject]{
var parameters:[String:AnyObject] = [String:AnyObject]()
parameters["lat"] = lLat!
parameters["lng"] = lLon!
if category != nil { // here is the problem
parameters["category"] = category!
}
return parameters
}
I have a category filter, if there is a value in category variable, i want to send it as QueryParam (should encoding be .URL? but how can i send json object ??)
this code does not work
if category != nil {
parameters["category"] = category!
}
How can i do this? Hope I can explain it clearly
Thanks in advance
You could solve it this way:
let mutableUrlRequest = NSMutableUrlRequest(URL: URL.URLByAppendingPathComponent(path)
mutableUrlRequest.HTTPMethod = .POST
let request = Alamofire.ParameterEncoding.URL.encode(mutableUrlRequest, parameters: createQueryParameters()).0
Alamofire.request(request)
However, I would advise you to look into the Router declaration of Alamofire and try this one. With it you can create dynamic requests and all of them are declared in a single file.
Edit:
Oh wait you can forget the previous edit the solution is quite simple and you also answered it by yourself. Yes you just have to change the encoding to .URL, you still are able to send json objects, because Alamofire itself decodes then the json object to a string for queryparams.
Alamofire.request(.POST, path, parameters:createQueryParams(), encoding: .URL).responseArray...
Edit 2:
Since the first edit did not work, try this:
let url = NSURL(string: path)!
let urlRequest = NSURLReqeust(URL: url)
request.HTTPMethod = "Post"
let encoding = Alamofire.ParameterEncoding.URL
let request = encoding.encode(urlRequest, createQueryParams())
Alamofire.request(request)

AFNetworking and Swift - Save json response

I want to make a GET request in swift to get some Json data.
I tried to use AFNetworking and it works, but I don't know how to return the Json I get.
I tried with a return but it's made before the GET so I get nothing...
func makeGet(place:String) -> String
{
var str:String = ""
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer.setValue("608c6c08443c6d933576b90966b727358d0066b4", forHTTPHeaderField: "X-Auth-Token")
manager.GET("http://something.com/api/\(place)",
parameters: nil,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
str = "JSON: \(responseObject.description)"
println(str) //print the good thing
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
str = "Error: \(error.localizedDescription)"
})
return str //return ""
}
Can you help me ?
Since you want to return the value after the webservice request is completed you have to pass the data via a delegate or a block(In swift it is called closures)
I see blocks useful here
//Above your class file create a handler alias
typealias SomeHandler = (String! , Bool!) -> Void
func makeGet(place:String , completionHandler: SomeHandler!)
{
var str:String = ""
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer.setValue("608c6c08443c6d933576b90966b727358d0066b4", forHTTPHeaderField: "X-Auth-Token")
manager.GET("http://something.com/api/\(place)",
parameters: nil,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
str = "JSON: \(responseObject.description)"
println(str) //print the good thing
completionHandler(str,false) //str as response json, false as error value
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
str = "Error: \(error.localizedDescription)"
completionHandler("Error",true)
})
//return str //return "" You don't want to return anything here
}
When you want to call the method get the values like this
makeGet(){
yourJSONString , errorValue in //Here the value will be passed after you get the response
if !errorValue {
println("The End."
}
}
More about swift closures
FYI: AFNetworking owner has created a new Networking layer for swift and it is called Alamofire (AF from AFNetworking is Alamofire :])
You're not getting a response from that function, because the GET operation happens asynchronously. That is, the order of execution looks like this:
You call makeGet
makeGet creates manager, which fires off a GET request
makeGet finishes executing and returns an empty string
(some time later) manager receives a value back from the server and executes either the success or failure block.
So the only time you have access to the JSON that comes back from the server is in step 4, and you need to find a way of storing that value so you can parse it or use it or whatever. There are a variety of options here -- one is to define closures that call event handlers on your class instance, like this:
class MyClass {
func jsonLoaded(json: String) {
println("JSON: \(json)")
}
func jsonFailed(error: NSError) {
println("Error: \(error.localizedDescription)")
}
func makeGet(place:String) {
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer.setValue("608c6c08443c6d933576b90966b727358d0066b4", forHTTPHeaderField: "X-Auth-Token")
manager.GET("http://something.com/api/\(place)",
parameters: nil,
success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
self.jsonLoaded(responseObject.description)
},
failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
self.jsonFailed(error)
}
)
}
}
let manager = AFHTTPSessionManager()
manager.GET("http://api.androidhive.info/json/movies.json", parameters: nil, success: { (operation, responseObject) -> Void in
print(responseObject)
}, failure: nil)
AFHTTPRequestOperationManager is not available in latest AFnetworking library, it has replaced with AFHTTPSessionManager.
This is a simple example of getting response object.