Skip to content

Instantly share code, notes, and snippets.

@HackingGate
Created June 20, 2019 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HackingGate/2151744247099e725934f0c924b70acb to your computer and use it in GitHub Desktop.
Save HackingGate/2151744247099e725934f0c924b70acb to your computer and use it in GitHub Desktop.
Test your API
// Test Webhook
// https://app.mailgun.com/app/webhooks
// WTFPL License
import Foundation
// Postbin URL
let urlString = "http://bin.mailgun.net/d806fafc"
// Sample Parameter: signature
let mailgunSignatureDict: [String: Any] = [
"timestamp": "1561003406",
"token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"signature": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
extension String {
// Convert Dictionary to JSON String
static func convert(dict: [String: Any]) -> String? {
do {
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
let jsonText = String(data: jsonData, encoding: .ascii)
return jsonText
} catch {
print(error.localizedDescription)
return nil
}
}
}
// JSON String
let jsonText = String.convert(dict: mailgunSignatureDict)
var url = URLComponents(string: urlString)!
url.queryItems = [
URLQueryItem(name: "signature", value: jsonText),
URLQueryItem(name: "testURLQueryItem", value: "testValue")
]
var request = URLRequest(url: url.url!)
request.httpMethod = "POST"
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print(error)
}
guard let data = data else {
return
}
if let response = response {
print(response)
}
if let json = try? JSONSerialization.jsonObject(with: data, options: []) {
print(json)
}
}.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment