我如何在Alamofire API路由器中添加多部分表单数据?

问题描述 投票:0回答:1

我有一个API路由器是这样的。我使用Alamofire 5.0编写。

import Alamofire

enum AlamofireRouterNetwork: URLRequestConvertible {
    case register(parameters: Parameters)
    case login(parameters: Parameters)
    // other cases
    var method: HTTPMethod {
        switch self {
        case .register:
            return .post
        case .login:
            return .post
        }
    }
    var path: String {
        switch self {
        case .register:
            return "/auth/register"
        case .login:
            return "/auth/login"
    }

    func asURLRequest() throws -> URLRequest {
        var urlRequest = URLRequest(url: myURL)
        urlRequest.httpMethod = method.rawValue
        switch self {
        case .register, .login:
            urlRequest.allHTTPHeaderFields = ["Content-Type": "application/json"]
        }

        switch self {
        case .register(let parameters), .login(let parameters):
            urlRequest = try JSONEncoding.default.encode(urlRequest, with: parameters)
        default:
            break
        }
        return urlRequest
    }
}

是否有一个 聪明的 如何在我的路由器中添加一个多表单数据请求?

swift alamofire multipartform-data alamofire-upload
1个回答
0
投票

这是我的解决方案,创建一个新的方法来获取多部分。我希望它能帮助你

enum APIRouter: URLRequestConvertible {
    case getSomething(id: String)
    case uploadSomething(data: Data)

    static let baseURLString = K.ProductionServer.apiBaseURL

    var method: HTTPMethod {
        switch self {
        case .getSomething:
            return .get
        case .uploadSomething:
            return .post
        }
    }

    var path: String {
        switch self {
        case .getSomething(let id):
            return "something/\(id)"
        case .uploadSomething:
            return "something/"
        }
    }

    var body: Parameters {
        var body: Parameters = [:]
        switch self {
        case .getSomething(let id):
            body["id"] = id
        default:
            break
        }

        return body
    }

    // MARK: URLRequestConvertible
    func asURLRequest() throws -> URLRequest {
        let url = try APIRouter.baseURLString.asURL()

        var urlRequest = URLRequest(url: url.appendingPathComponent(path))
        urlRequest.httpMethod = method.rawValue

        // Common Headers
        urlRequest.setValue(ContentType.json.rawValue, forHTTPHeaderField: HTTPHeaderField.acceptType.rawValue)

        // Encode body
        switch self {
        default:
            //urlRequest.setValue(ContentType.json.rawValue, forHTTPHeaderField: HTTPHeaderField.contentType.rawValue)
            urlRequest = try JSONEncoding.default.encode(urlRequest, with: body)
        }

        return urlRequest
    }

    // MARK: MultipartFormData
    var multipartFormData: MultipartFormData {
        let multipartFormData = MultipartFormData()
        switch self {
        case .uploadSomething(let data):
            multipartFormData.append(data, withName: "file", fileName: "file.png", mimeType: "image/png")
        default: ()
        }

        return multipartFormData
    }
}

例如:

let route = APIRouter.uploadSomething(data: Data())
self.session
.upload(multipartFormData: route.multipartFormData, with: route)
.uploadProgress(closure: { (progress) in
   print("\(progress)")
})
.validate()
.responseData(completionHandler: { (response) in
   print("\(response)")
})
© www.soinside.com 2019 - 2024. All rights reserved.