Skip to content

Instantly share code, notes, and snippets.

@akrabat
Created March 5, 2017 19:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akrabat/bfa38877a61e51fcd07086226166c52b to your computer and use it in GitHub Desktop.
Save akrabat/bfa38877a61e51fcd07086226166c52b to your computer and use it in GitHub Desktop.
OpenWhisk action to POST to a server
import KituraNet
import SwiftyJSON
func main(args: [String:Any]) -> [String:Any] {
let url = "http://httpbin.org/post"
let dataToSend = ["foo": ["bar": "baz"]]
var result: [String:Any] = ["result": "No response"]
postJsonTo(url, data: dataToSend) { response in
do {
let str = try response!.readString()!
if let jsonDictionary = JSON.parse(string: str).dictionaryObject {
result = jsonDictionary
}
} catch {
print("Error \(error)")
}
}
return result
}
func postJsonTo(_ url: String, data: [String:Any], callback: @escaping ClientRequest.Callback) {
let jsonBody = WhiskJsonUtils.dictionaryToJsonString(jsonDict: data) ?? ""
let base64Body = Data(jsonBody.utf8)
postTo(url, body: base64Body, headers: ["Content-Type": "application/json"], callback: callback)
}
func postTo(_ url: String, body: Data, headers: [String: String], callback: @escaping ClientRequest.Callback) {
var options: [ClientRequest.Options] = [
.schema(""),
.method("POST"),
.hostname(url),
.headers(headers)
]
var result: [String:Any] = ["result": "No response"]
let request = HTTP.request(options, callback: callback)
request.write(from: body)
request.end() // send request
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment