Загрузка файла STL с Alamofire в Octoprint через API

Я застрял, пытаясь создать функцию для загрузки файла (STL) в Octoprint API с помощью Alamofire в быстром приложении iOS. Все мои другие функции 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 }

1 ответ

Я получил это работает, разместив код на случай, если он поможет кому-то еще. Это был мой первый раз, когда я использовал многочастный 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)
            }
    }
    )
}
Другие вопросы по тегам