-
-
Save akrabat/bfa38877a61e51fcd07086226166c52b to your computer and use it in GitHub Desktop.
OpenWhisk action to POST to a server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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