- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直试图在 swift iOS 应用程序中使用 Alamofire 创建一个将文件 (STL) 上传到 Octoprint API 的函数。我的所有其他 API 函数都运行良好,因此我假设我的上传问题与所需的多部分数据有关。我不断收到 403 错误。
我当前的代码:
func uploadFile(printer: Printers, localFileURL: URL, completion: @escaping (JSON) -> ()) {
let url = printer._ipaddress! + "api/files/local"
let key = printer._apikey!
let boundary = "Boundary-\(UUID().uuidString)"
let contentType = "multipart/form-data"
let fileData = try! String(contentsOf: localFileURL).data(using: String.Encoding.utf8, allowLossyConversion: false)
Alamofire.upload(
multipartFormData: { (multipart) in
multipart.boundary = boundary
multipart.contentType = contentType
multipart.append(key.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "X-Api-Key")
multipart.append(fileData!, withName: "file", fileName: localFileURL.lastPathComponent, mimeType: "application/octet-stream")
},
to: url,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
}
错误消息:
responseValidationFailed(reason: Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(code: 403))
2019-08-22 18:34:51.120426-0400 iOSApp[32279:979456] [] nw_socket_handle_socket_event [C9:2] Socket SO_ERROR [54: Connection reset by peer]
[Request]: POST http://192.168.2.14/api/files/local
[Response]: <NSHTTPURLResponse: 0x600001f28fe0> { URL: http://192.168.2.14/api/files/local } { Status Code: 400, Headers {
"Content-Length" = (
73
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
Date = (
"Thu, 22 Aug 2019 22:34:51 GMT"
);
"X-Robots-Tag" = (
"noindex, nofollow, noimageindex"
);
} }
[Data]: 73 bytes
[Result]: FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
[Timeline]: Timeline: { "Request Start Time": 588206091.090, "Initial Response Time": 588206091.092, "Request Completed Time": 588206091.125, "Serialization Completed Time": 588206091.125, "Latency": 0.002 secs, "Request Duration": 0.035 secs, "Serialization Duration": 0.000 secs, "Total Duration": 0.035 secs }
最佳答案
我让它工作了,发布代码以防它对其他人有帮助。这是我第一次使用多部分 POST,所以我只是错误地构建了正文和标题。使用 Postman 运行测试确实帮助我更好地理解了。
func uploadFile(printer: Printers, localFileURL: URL, completion: @escaping (JSON) -> ()) {
let url = printer._ipaddress! + "api/files/local"
let key = printer._apikey!
let boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"
let mimeType = "application/octet-stream"
let fileData = try! String(contentsOf: localFileURL)
let header = HTTPHeaders(dictionaryLiteral: ("X-Api-Key", key), ("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"))
Alamofire.upload(
multipartFormData: { (multipart) in
multipart.append(boundary.data(using: String.Encoding.utf8)!, withName: "bounday")
multipart.append(fileData.data(using: String.Encoding.utf8)!, withName: "file", fileName: localFileURL.lastPathComponent, mimeType: mimeType)
multipart.append(boundary.data(using: String.Encoding.utf8)!, withName: "bounday")
multipart.append("false".data(using: String.Encoding.utf8)!, withName: "select")
multipart.append(boundary.data(using: String.Encoding.utf8)!, withName: "bounday")
multipart.append("false".data(using: String.Encoding.utf8)!, withName: "print")
},
to: url,
headers: header,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
}
关于ios - 通过 API 使用 Alamofire 将 STL 文件上传到 Octoprint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57617824/
我正在使用 Octoprint Javascript API ( http://docs.octoprint.org/en/master/jsclientlib/index.html ) 制作一个仪表
我想做的是使用 OctoPrint Rest API。但是,当我尝试执行需要命令的 POST 请求时,我不断遇到错误。这是文档中的一个示例。 POST /api/connection HTTP/1.1
背景 规范: OctoPrint 1.3.4 (master branch) Apache/2.4.10 (Debian) using mod_proxy 我正在尝试在我的 Apache 服务器上运行
我想通过 Apache HttpClient 向 Octoprint API 发送一个 POST 请求,如下所示:http://docs.octoprint.org/en/master/api/job
我一直试图在 swift iOS 应用程序中使用 Alamofire 创建一个将文件 (STL) 上传到 Octoprint API 的函数。我的所有其他 API 函数都运行良好,因此我假设我的上传问
我是一名优秀的程序员,十分优秀!