gpt4 book ai didi

ios - 无法识别 POST 正文值

转载 作者:行者123 更新时间:2023-11-28 12:16:30 24 4
gpt4 key购买 nike

我正在调用 POST API,但无法识别发布值。我正在使用 alamofire 进行 API 处理

 public func POSTRequest(postData : AnyObject?,APIName : String?,headers: HTTPHeaders?,success:@escaping (JSON) -> Void, failure:@escaping (String,String) -> Void)
{
let url = URL(string: APIName!)!
var urlRequest = URLRequest(url: url)

urlRequest.httpBody = postData as? Data
urlRequest.httpMethod = "POST"
urlRequest.allHTTPHeaderFields = headers

let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 30.0
manager.request(urlRequest).responseJSON { ( responseData) -> Void in

if responseData.result.isSuccess {
let statusCode = responseData.response?.statusCode
if(statusCode == 200){
let resJson = JSON(responseData.result.value!)
success(resJson)
}
else {
let resJson = JSON(responseData.result.value!)
let errorMsg = resJson["message"].stringValue
let errorTitle = resJson["title"].stringValue
failure(errorMsg,errorTitle)
}
}
if responseData.result.isFailure {
let error : Error = responseData.result.error!
failure(error.localizedDescription,"")
}
}
}



// MARK: Login
func userLogin(postData: AnyObject, completionHandler:@escaping (JSON) -> Void,failure:@escaping (String,String) -> Void)
{
let apiName = GlobalConstants.rootUrl + GlobalConstants.loginUrl

let httpHeaders: HTTPHeaders = getHeaders()

ApiRequestHandler.shared.POSTRequest(postData: postData, APIName: apiName, headers: httpHeaders, success: { (JSON) in
print("JSON \(JSON)")
completionHandler(JSON)
})
{ (Errormsg,ErrorTitle) in
print("Error \(Errormsg)")
failure(Errormsg,ErrorTitle)
}
}

我在这里创建帖子数据

        let postDataDct = NSMutableDictionary()
postDataDct.setValue(self.userNameTextField.text, forKey: "username")
postDataDct.setValue(self.phoneNumberTextField.text, forKey: "mobile")

guard let data = try? JSONSerialization.data(withJSONObject:
postDataDct , options: JSONSerialization.WritingOptions.prettyPrinted)
as NSData else {
return;
}
print(NSString(data: data as Data, encoding:
String.Encoding.utf8.rawValue)! as String)

在这里创建标题

func getHeaders() -> HTTPHeaders {
let headers: HTTPHeaders = ["Content-Type": "application/json" ,"Authorization" : "Basic dGFuaXNocWdfgaFF%waXJvb3Q6dzBEMFlkSnR0"]
return headers
}

我这里调用的是登录方法

   ApiManager.shared.userLogin(postData: postDataDct as AnyObject, completionHandler: { (JSON) in
print(JSON)
})
{ (ErrorMsg, ErrorTitle) in
print(ErrorMsg)
}

请帮我找出问题。

API 给我错误响应,提示用户名或密码为空。

最佳答案

对于发布 Alamofire 有它自己的参数类尝试像这样使用与 make share 一样使用服务器上的确切 key 。

let parameter: Parameters = [
"Email": txtfld_email.text!,
"Password": txtfld_password.text!
]

关于ios - 无法识别 POST 正文值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com