stoneron

RapidAPI-Translate

Oct 7th, 2020 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Foundation
  2.  
  3. let headers = [
  4.     "x-rapidapi-host": "google-translate1.p.rapidapi.com",
  5.     "x-rapidapi-key": "78767832e6mshd1b6d5b2fa681a2p1229e3jsn09a1a1972cad",
  6.     "accept-encoding": "application/gzip",
  7.     "content-type": "application/x-www-form-urlencoded"
  8. ]
  9.  
  10. let postData = NSMutableData(data: "source=en".data(using: String.Encoding.utf8)!)
  11. postData.append("&q=Hello, world!".data(using: String.Encoding.utf8)!)
  12. postData.append("&target=es".data(using: String.Encoding.utf8)!)
  13.  
  14. let request = NSMutableURLRequest(url: NSURL(string: "https://google-translate1.p.rapidapi.com/language/translate/v2")! as URL,
  15.                                         cachePolicy: .useProtocolCachePolicy,
  16.                                     timeoutInterval: 10.0)
  17. request.httpMethod = "POST"
  18. request.allHTTPHeaderFields = headers
  19. request.httpBody = postData as Data
  20.  
  21. let session = URLSession.shared
  22. let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  23.     if (error != nil) {
  24.         print(error)
  25.     } else {
  26.         let httpResponse = response as? HTTPURLResponse
  27.         print(httpResponse)
  28.     }
  29. })
  30.  
  31. dataTask.resume()
Add Comment
Please, Sign In to add comment