我已经设置了一个 Perfect服务器,我已经编写了我所有的 API,但我被困在 CURL 上,我不知道如何设置我以前从未使用过 CURL 的 header 。
我正在设置支付网关,我想设置授权 header 并发送正文数据,但我不知道如何操作。
来自示例 http://perfect.org/docs/cURL.html
let curlObject = CURL(url: "http://www.perfect.org")
curlObject.perform {
code, header, body in
print("Request error code \(code)")
print("Response: \(curlObject.responseCode)")
print("Header: \(header)")
print("Body: \(body)")
}
我提到了这个但没有得到帮助怎么办?
最佳答案
经过大量搜索,我发现了一些东西
let url = "https://httpbin.org/post"
let postParamString = "key1=value1&key2=value2"
let byteArray = [UInt8](postParamString.utf8)
let curl = CURL(url: url)
let _ = curl.setOption(CURLOPT_POST, int: 1)
let _ = curl.setOption(CURLOPT_POSTFIELDS, v: UnsafeMutableRawPointer(mutating: byteArray))
let _ = curl.setOption(CURLOPT_POSTFIELDSIZE, int: byteArray.count)
curl.perform { code, header, body in
print("Request error code \(code) \n Response: \(curl.responseCode) \n Header: \(UTF8Encoding.encode(bytes:header)) \n Body: \(UTF8Encoding.encode(bytes: body))")
}
有关更多信息,请参阅此C 示例 https://curl.haxx.se/libcurl/c/example.html
关于swift - 如何使用 Perfect 在 CURL 中发出 post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41019796/