Created
June 20, 2019 07:10
-
-
Save HackingGate/2151744247099e725934f0c924b70acb to your computer and use it in GitHub Desktop.
Test your API
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
// 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