我已經寫了一個如下所示的 Alamofire 請求并獲得了成功的結果。
let urlString = "https://dev.thebeats.app/api/authorization/send_signup_email?lang=en"
let url = URL(string: urlString)!
AF.request(url, method: .post,
parameters: ["email": "[email protected]"],
encoding: URLEncoding.default,
headers: ["Content-type": "application/x-www-form-urlencoded"],
interceptor: nil, requestModifier: nil)
.response { result in
print(String(decoding: result.data!, as: UTF8.self))
}
這是回復
{
"status": 200,
"success": true,
"message": "Email send successfully",
"body": {
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhbGciOiJIUzI1NiIsImlkIjo1Nywic3ViIjo1NywibmFtZSI6bnVsbCwiaWF0IjoxNjU1OTc5NTczLCJleHAiOjE2NTYwNjU5NzN9.331-g9WCEy6OIVkdm4UnxOx3BnuEx1fNy59shiYBgvQ"
}
}
我試圖用 Moya 寫這個請求,但總是得到 404 錯誤。這是代碼。
extension AuthenticationService: TargetType {
var baseURL: URL {
return URL(string: "https://dev.thebeats.app/api")!
}
var path: String {
switch self {
case .registration:
return "/authorization/send_signup_email?lang=en"
}
}
var method: Moya.Method {
switch self {
case .registration:
return .post
}
}
var task: Task {
switch self {
case .registration(let email):
var params: [String: Any] = [:]
params["email"] = "[email protected]"
return .requestParameters(parameters: params, encoding: URLEncoding.default)
}
}
var headers: [String : String]? {
switch self {
case .registration:
return ["Content-type": "application/x-www-form-urlencoded"]
}
}
var validationType: ValidationType {
return .successCodes
}
}
這是使用的回應Moya
{
"message": "Controller class Api could not be found.",
"url": "/api/authorization/send_signup_email?lang=en",
"code": 404,
"file": "/var/www/html/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php",
"line": 340
}
請幫我弄清楚。提前致謝。
uj5u.com熱心網友回復:
你讓它張貼,所以這應該是
case .registration:
return "/authorization/send_signup_email"
和
params["email"] = "[email protected]"
params["lang"] = "en"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495243.html
