swift 框架moya post請求遇到的坑

找了一大圈,發(fā)現(xiàn)國內(nèi)根本沒有解決的方法,去國外翻了一遍才找到,記錄一下

重點(diǎn):? bodyEncoding: JSONEncoding.default

1.用moya測試登陸post接口,發(fā)現(xiàn)傳過去的沒有包裝成json,百度了好久,全是千篇一律的答案,stackoverflow上也沒找到,無奈去github的issue上翻,總算找到附上地址https://github.com/Moya/Moya/issues/1533。然后總結(jié)下經(jīng)歷,之前以為是header的content-type設(shè)置的問題,發(fā)現(xiàn)moya4.0更新了一個(gè)新的header參數(shù)可以設(shè)置header了,貌似以前是用一種復(fù)雜的方法設(shè)置的,叫什么endxxx的,沒仔細(xì)研究那個(gè)東西,反正用不上,用上的時(shí)候再說吧,然后把parameter參數(shù)放到了task中,其實(shí)剛開始的研究思路主要放在尋找moya中是否有一個(gè)屬性能設(shè)置json傳遞, .requestCompositeParameters(bodyParameters: parameters, bodyEncoding: JSONEncoding.default, urlParameters: [:]),然后就是這句話,主要在于JSONEncoding吧,這個(gè)是task中返回的。然后如果想用以前的parameters的話,可以自己寫一個(gè),然后把parameters傳遞給bodyParameters就行了。

貼上解決方法

import Moya

let loginProvider = NetworkUtility.getMoyaProvider(LoginAPI.self)

public enum LoginAPI {

? ? case loginUser([String: Any])

? ? case forgotPasswordOTP(String)

? ? case resetPassword([String: Any])

}

extension LoginAPI: TargetType {

? ? public var baseURL: URL { return URL(string: ConfigData.shared.getURL(forKey: DefaultsKeys.baseURL))! }

? ? public var path: String {

? ? ? ? switch self {

? ? ? ? case .loginUser:

? ? ? ? ? ? return ConfigData.shared.getURL(forKey: DefaultsKeys.login)

? ? ? ? case .resetPassword:

? ? ? ? ? ? return ConfigData.shared.getURL(forKey: DefaultsKeys.resetPassword)

? ? ? ? case .forgotPasswordOTP:

? ? ? ? ? ? return ConfigData.shared.getURL(forKey: DefaultsKeys.forgotPasswordOTP)

? ? ? ? }

? ? }

? ? public var method: Moya.Method {

? ? ? ? switch self {

? ? ? ? case .loginUser:

? ? ? ? ? ? return .post

? ? ? ? case .resetPassword, .forgotPasswordOTP:

? ? ? ? ? ? return .put

? ? ? ? }

? ? }

? ? public var task: Task {

? ? ? ? switch self {

? ? ? ? case .loginUser(let bodyParameters),

? ? ? ? ? ? ? //UserLogin dictionaryRepresentation is used for this

?? ? ? ? ? ? .resetPassword(let bodyParameters):

? ? ? ? ? ? return .requestCompositeParameters(bodyParameters: bodyParameters,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? bodyEncoding: JSONEncoding.default,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? urlParameters: [:])

? ? ? ? case .forgotPasswordOTP(let email):

? ? ? ? ? ? return .requestCompositeParameters(bodyParameters: [User.Keys.email: email],

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? bodyEncoding: JSONEncoding.default,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? urlParameters: [:])

? ? ? ? }

? ? }

? ? public var headers: [String: String]? {

? ? ? ? return NetworkUtility.getHeaders()

? ? }

? ? public var sampleData: Data {

? ? ? ? var data: Data! = nil

? ? ? ? switch self {

? ? ? ? case .loginUser:

? ? ? ? ? ? data = NetworkUtility.loadJSON(jsonName: "3.33.4Success")

? ? ? ? case .resetPassword:

? ? ? ? ? ? data = NetworkUtility.loadJSON(jsonName: "ResetPassword")

? ? ? ? case .forgotPasswordOTP:

? ? ? ? ? ? data = NetworkUtility.loadJSON(jsonName: "VerifyUser")

? ? ? ? }

? ? ? ? return data

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容