gpt4 book ai didi

swift - 等效的 Swift NSURLSession 或 Alamofire Curl 帖子

转载 作者:行者123 更新时间:2023-11-30 13:24:07 31 4
gpt4 key购买 nike

我的这个curl帖子工作得很好,但是在我的一生中我无法让它与NSURLSession或Alamofire一起工作。返回正常的 curl 是:

curl https://api.truevault.com/v1/vaults/XXXXXX-XXXX-XXXX-XXXX-XXXXXXXX/blobs \
-X POST \
-u yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyy: \
--form "file=@foo.pdf" \
-H "Content-Type:multipart/form-data"

无论我使用哪种curl 或 NSURLSession 变体,我都会收到 401 错误,但使用curl 会成功完成!请帮忙!谢谢!

当前 ALAMOFIRE 尝试

        let headers = ["Authorization": Config.trueVaultKey, "Content-Type": "multipart/form-data"]

Alamofire.upload(
Alamofire.Method.POST,
url!,
headers: headers,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(
data: dat!,
name: "file",
fileName: "license.png",
mimeType: "image/png"
)
},
encodingCompletion: { encodingResult in
switch encodingResult {

case .Success(let upload, _, _):
upload.responseString { response in
//JSON = response.result.value! as String
debugPrint(response)

print("\n\n\nREQUEST HEADER: \(response.request!.allHTTPHeaderFields!)")
print("\n\n\nREQUEST BODY: \(response.request!.HTTPBody)")
print("\n\n\nREQUEST BODY STREAM: \(response.request!.HTTPBodyStream)")
}
case .Failure(let encodingError):
print(encodingError)
}
})

最佳答案

尝试像这样构建您的网址:

 let userAuth = "username:password" // credential
let url = "https://\(userAuth)@api.truevault.com/v1/vaults/XXXXXX-XXXX-XXXX-XXXX-XXXXXXXX/blobs" // notice the at(@) sign

不要设置标题,它们会自动设置。

关于swift - 等效的 Swift NSURLSession 或 Alamofire Curl 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37425425/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com