Sorry for my bad english :)
So here we go. I started to create iPhone Apps in Swift 2.
I would like to use an API. For the Request I have used the Alamofire 3.0 Library. And this works fine, but I am unable to handle the JSON. I tried to use SwiftyJSON but I have no idea how it works. There is my code:
let headers = [
"Content-Type": "application/json"
]
Alamofire.request(.GET, "API URL", headers: headers)
.responseJSON { response in
if response.result.isSuccess {
}
}
I hope someone can help me. ;) Thanks
My latest usage of Alamofire (3.0) and SwiftyJSON (2.3)
let parameters = ["param1" : param, "param2" : "stringParam"] // my params, not required
Alamofire.request(.POST, url, parameters: parameters)
.responseJSON{ response in
guard response.result.error == nil else {
print("Error. \(response.result.error?.localizedDescription)")
return
} // guard close
if response.result.isSuccess {
let post = JSON(response.result.value!) // JSON is stored in post variable
// Another check of result
//if let value: AnyObject = response.result.value {
// let post = JSON(value)
} // if close
} // responseJSON close
Then access the JSON as per the instructions in Github
Hope this helps
Jacko
Related
So, I'm trying to make a very simple watchOS app in XCode. It consists of a button, two labels and a separator between the two labels. It is a digital assistant app, and needs to interface with Dialogflow (https://dialogflow.com).
The button calls the presentTextInputController function, and I want to use that result as a query to my Dialogflow agent.
I need to make an HTTP request, which in JS would look more like this:
{
url:"https://api.api.ai/v1/query",
method:"post",
body:JSON.stringify({query:"userInput",lang:"en-US",sessionID:"yaydevdiner"}),
headers:{
contentType:"application/json; charset=utf-8",
Authorization:"Bearer <auth_token>"
}
}
The response is a JSON object, and I need to access the jsonObject["result"]["speech"] value as a String to use Label.setText()
Everything I've tried has given errors about type Any and other such things. I also haven't been able to do much debugging since the print output isn't showing up in XCode.
I must mention that I'm an extreme beginner to Swift, and I am not good at handling their types and casting and unpacking and things like that.
Could someone show me how I might handle this request and the subsequent processing of the JSON?
Here is my current code:
//HTTP Request
let parameters = [
"query":name![0] as? String,
"lang":"en-US",
"sessionID":"yaydevdiner"
];
//create the url with URL
let url = URL(string: "https://api.api.ai/v1/query")! //change the url
//create the session object
let session = URLSession.shared
//now create the URLRequest object using the url object
var request = URLRequest(url: url)
request.httpMethod = "POST" //set http method as POST
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
} catch let error {
print(error.localizedDescription)
}
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("Bearer f786fef55008491fb8422cea2be85eb1", forHTTPHeaderField: "Authorization")
//create dataTask using the session object to send data to the server
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
do {
//create json object from data
if let json = try JSONSerialization.jsonObject (with: data, options: .mutableContainers) as? [String:Any] {
self.Response.setText(json["result"]["string"]);
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}
Response is a text label.
This code gives me an error saying I should have a question mark between
json["result"] and ["speech"]. When I do this, it gives me another error saying "Type Any has no subscript members".
Ok, I figured it out.
Because XCode automatically makes an iOS app with the watchOS app, I decided to try debugging in the iOS app until I got the HTTP request and JSON parsing right.
Inside the JSONSerialization if statement, I had to add another if statement:
if let result = responseJSON["result"] as? [String:Any]{
self.Response.setText(result!["speech"] as? String ?? "Network error Occurred")
}
Thanks for the help from vadian!
I am trying to subscribe new user to Mailchimp list using Alamofire.
Problem starts when I'm trying to subscribe new user with .post method and JSONObject as a parameter:
func subscribeMail(){
let credentialData = "<my_api_key>".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString(options: [])
let headers = ["Authorization": "Basic \(base64Credentials)"]
let url = "https://us11.api.mailchimp.com/3.0/lists/<my_list_id>/members/"
let jsonObj: [String: AnyObject] = [
"mail_address" : "testMailAddress#gmail.com" as AnyObject,
"status" : "subscribed" as AnyObject,
]
let valid = JSONSerialization.isValidJSONObject(jsonObj)
print(valid)
Alamofire.request(url, method: .post, parameters: jsonObj , encoding: URLEncoding.default , headers: headers).responseJSON{response in
if response.result.isFailure {
print("Failed")
}
else if (response.result.value as? [String: AnyObject]) != nil {
print(response)
}
}
}
I get back status code 400:
SUCCESS: {
detail = "We encountered an unspecified JSON parsing error.";
instance = "";
status = 400;
title = "JSON Parse Error";
type = "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/";
}
In Mailchimp documentation:
JSONParseError
We encountered an unspecified JSON parsing error.
This error means that your JSON was formatted incorrectly or was considered invalid or incomplete.
As you can see I am checking my jsonObj if its valid. So I dont get this parsing error..
In Mailchimp API 3.0 its written that just email_address and status fields are needed to subscribe new mail.
If I try to to send request with Alamofire using .get method with some mail address that is already subscribed, everything works fine, I can receive all data from Mailchimp.
Is there really problem with my jsonObj or is it somewhere else?
Change the object key from 'mail_address' to 'email_address' and give a try.
let jsonObj: [String: AnyObject] = [
"email_address" : "testMailAddress#gmail.com" as AnyObject,
"status" : "subscribed" as AnyObject,
]
Since you're getting a JSONParseError, your issue is related to the format in which you're sending the parameters.
Try encoding: JSONEncoding.default instead of encoding: URLEncoding.default.
I am trying to learn Swift. One of my projects is to try to retrieve JSON data from an internal web service (a group of Python CGI scripts) and convert it into a Swift object. I can do this easily in Python, but I am having trouble doing this in Swift. Here is my playground code:
import UIKit
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let endpoint: String = "http://pathToCgiScript/cgiScript.py"
let url = NSURL(string: endpoint)
let urlrequest = NSMutableURLRequest(URL: url!)
let headers: NSDictionary = ["User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)",
"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"]
urlrequest.allHTTPHeaderFields = headers as? [String : String]
urlrequest.HTTPMethod = "POST"
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(urlrequest) {
(data, response, error) in
guard data != nil else {
print("Error: did not receive data")
return
}
guard error == nil else {
print("Error calling script!")
print(error)
return
}
do {
guard let received = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as?
[String: AnyObject] else {
print("Could not get JSON from stream")
return
}
print(received)
} catch {
print("error parsing response from POST")
}
}
task.resume()
I know making a 'POST' to retrieve data may look odd, but that is how the system is set up. I keep on getting:
Could not get data from JSON
I checked the response, and the status is 200. I then checked the data's description with:
print(data?.description)
I got an unexpected result. Here is a snippet:
Optional("<0d0a5b7b 22535441 54555322 3a202244 6f6e6522 2c202242 55535922...
I used Mirror, and apparently the type is NSData. Not sure what to make of this. I have tried to encode the data with base64EncodedDataWithOptions. I have tried different NSJSONReadingOptions as well to no avail. Any ideas?
Update:
I used Wireshark to double check the code in the Playground. Not only was the call made correctly, but the data being sent back is correct as well. In fact, Wireshark sees the data as JSON. The issue is trying to turn the JSON data into a Swift object.
I figured out what was wrong. I was casting to the wrong type. This is the new code:
guard let received = try! NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) as? [AnyObject]
The JSON was not returning an array of dictionaries but an array of objects.
I read answers in SO and checked the official Alamofire documentation but most of these answers increase the confusion whether the JSON data gets added to the URL parameters or the body.
The HTTP request must contain a parameter key, which is equal to my API key to access the API and the JSON containing the image data and some data related to the API.
the URL with the request parameter looks like this:
https://vision.googleapis.com/v1/images:annotate?key=My_API_Key
and the JSON structure is at This link.
My Swift code looks like this.
import SwiftyJSON
import Alamofire
// all imports are done
func requestAPI(body:JSON) -> JSON {
let APIKey:String = "My_API_KEY"
let header = ["Content-Type":"application/json"]
let url:String = "https://vision.googleapis.com/v1/images:annotate"
let parameters = ["key":APIKey]
var response = Alamofire.request(.POST,url,headers:header,parameters:parameters)
response.validate().responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
print("JSON: \(json)")
print(response.request)
}
case .Failure(let error):
print(error)
}
}
return body //dummy return
}
I did go through the Alamofire documentation.
From the
POST Request With URL-Encoded Parameters
section of the documentation,
let parameters = [
"foo": "bar",
"baz": ["a", 1],
"qux": [
"x": 1,
"y": 2,
"z": 3
]
]
Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters)
// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3
My understanding of this is that the JSON passed as the parameters argument is added as the parameters to the URL (similar to the key:My_API_Key part of my request) for the HTTp POST request. and using the .JSON as an encoding option adds the parameters argument as a JSON body for the request. How do I do both (A parameter key that is to be URL encoded and the JSON data that is to be added to the HTTP body)? the request method of Alamofire seems to be doing either of the 2 using the same argument.
I tried creating a NSURLMutableRequest and adding the body. but while accessing the rawValue of a SwiftyJSON JSON object, I get the following error.
error: call can throw, but it is not marked with 'try' and the error is not handled
if let bodyData:NSData = body.rawData(){
this is the code.
func requestAPI(body:JSON) -> JSON {
let APIKey:String = "My_API_Key"
let header = ["Content-Type":"application/json"]
let url = NSURL(string:"https://vision.googleapis.com/v1/images:annotate")
let parameters = ["key":APIKey]
var request = NSMutableURLRequest(URL:url!)
request.HTTPMethod = "POST"
if let bodyData:NSData = body.rawData(){
request.HTTPBody = bodyData
}
let encoding = Alamofire.ParameterEncoding.URL
(request, _) = encoding.encode(request, parameters: parameters) // Parameters are added here
var response = Alamofire.request(.POST,url!,headers:header,parameters:parameters)
response.validate().responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
print("JSON: \(json)")
//return json
print(response.request)
}
case .Failure(let error):
print(error)
}
}
return body //dummy return
}
Where and how do I set both the HTTP body of the request to JsonData and encode the URL with the parameters?
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)