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?