Answer the question
In order to leave comments, you need to log in
How to add. key value in moya?
Good afternoon, I need to send a post request with a key= value string parameter in . moya
//
import UIKit
import Foundation
import Moya
import Alamofire
let tmProvider = MoyaProvider<APIEndpoint>(plugins: [NetworkLoggerPlugin(verbose: true, responseDataFormatter: JSONResponseDataFormatter)])
public enum APIEndpoint {
// Auth
case signUp(params: [String: Any])
case signIn(params: [String: Any])
case forgotPassword(params: [String: Any])
}
extension APIEndpoint: TargetType {
public var baseURL: URL { return URL(string: "")! }
public var path: String {
switch self {
case .signIn:
return "/drivers/login"
}
public var method: Moya.Method {
switch self {
case .signIn, .signUp, .forgotPassword, .addComment, .uploadReportImage,
.addReport, .addFavorite, .getFavorites, .removeFavorite, .uploadAvatar,
.getProfile, .updateProfile, .getUserComments:
return .post
default:
return .get
}
}
public var parameters: [String: Any]? {
var params:[String: Any] = [:] // for post params
var query:[String: Any] = [:] // for get params
public var parameterEncoding: ParameterEncoding {
switch self {
case .addComment, .uploadReportImage,
.addReport, .addFavorite, .getFavorites, .removeFavorite, .uploadAvatar,
.getProfile, .updateProfile, .getUserComments:
return CompositeJsonEncoding()
default:
return CompositeEncoding()
}
}
public var sampleData: Data {
return "Default sample data".data(using: String.Encoding.utf8)!
}
public var task: Task {
switch self {
case .uploadAvatar(let data):
let imgData = MultipartFormData(provider: MultipartFormData.FormDataProvider.data(data), name: "file", fileName: "userAva.jpg", mimeType: "image/jpeg")
let multipartData = [imgData]
return .uploadMultipart(multipartData)
case .uploadReportImage(let data):
let imgData = MultipartFormData(provider: .data(data), name: "file", fileName: "userAva.jpg", mimeType: "image/jpeg")
let multipartData = [imgData]
return .uploadMultipart(multipartData)
default:
return .requestPlain
}
}
public var validate: Bool {
return true
}
public var headers: [String : String]? {
var params:[String: String] = [:] // for post params
params["Authorization"] = "Basic ZGV2QGFtb3RvcnMua3o6MTIzMTIz"
return params
}
}
public func url(_ route: TargetType) -> String {
return route.baseURL.appendingPathComponent(route.path).absoluteString
}
struct CompositeEncoding: ParameterEncoding {
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
guard let parameters = parameters else {
return try urlRequest.asURLRequest()
}
let queryParameters = (parameters["query"] as! Parameters)
var queryRequest = try URLEncoding(destination: .queryString).encode(urlRequest, with: queryParameters)
if let body = parameters["body"] {
let bodyParameters = (body as! Parameters)
let req = try URLEncoding(destination: .queryString).encode(urlRequest, with: bodyParameters)
if let url = req.url, let query = url.query {
queryRequest.httpBody = query.data(using: .utf8)
}
return queryRequest
} else {
return queryRequest
}
}
}
Answer the question
In order to leave comments, you need to log in
The devil himself will break his leg. It should probably be something like this. parameters and there are your key=val. Why write public everywhere? Why include Foundation if you already have UIKit? Is it the same with Alamofire?
public var parameters: [String: Any]? {
var params:[String: Any] = [:] // for post params
var query:[String: Any] = [:] // for get params
}
public var parameterEncoding: ParameterEncoding {
switch self {
case .addComment, .uploadReportImage,
.addReport, .addFavorite, .getFavorites, .removeFavorite, .uploadAvatar,
.getProfile, .updateProfile, .getUserComments:
return CompositeJsonEncoding()
default:
return CompositeEncoding()
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question