-
Notifications
You must be signed in to change notification settings - Fork 0
/
makePOSTRequest.swift
37 lines (31 loc) · 1.07 KB
/
makePOSTRequest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import UIKit
let API_KEY = "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"
let prompt = "A cute baby sea otter"
func makePOSTRequest() {
guard let url = URL(string: "https://api.openai.com/v1/images/generations") else {
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(API_KEY)", forHTTPHeaderField: "Authorization")
let body: [String: AnyHashable] = [
"prompt" : prompt,
"n" : 10,
]
request.httpBody = try? JSONSeriaization.data(withJSONObject: body, options: .fragmentAllowed)
let task = URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data, error == nil else {
return
}
do {
let response = try JSONSeriaization.jsonObject(with: data, options:.allowFragments)
print("SUCESS: \(response)")
}
catch {
print(error)
}
}
task.resume()
}
makePOSTRequest()