i have created network layer using moya and i m trying to make post method with "Content-Type": "application/x-www-form-urlencoded".
what will be the Encoding type for perameter . Now i am using JSONEncoding.default its not working properly .
case .setAuthentication(let security_code):
return .requestParameters(parameters: ["security_code":"\(security_code)"], encoding: JSONEncoding.default )
Problem :
I m setting user password to newly created account and i get responce with this method but when i m going to log in with this password its saying wrong password . even i see in backend same password .
Hey @ekramramu, that would be true for URLEncoding when the parameters are encoded in body. This happens in two cases:
URLEncoding.default encoding and the request is not .get, .head or .delete (so with .post you get the parameters in body)URLEncoding.httpBody encoding (this one doesn't care about method of your request)let me know if it helps!
@sunshinejr thank you firstly.
yeah I am using post method. I m getting same message (password incorrect ) even i use JSONEncoding.default insted of URLEncoding.httpBody .And i need it to post method .here is
my full layer :
ServiceAPI :
enum ServiceAPI {
case setAuthentication(security_code:String,auth_token:String)
}
extension ServiceAPI: TargetType,AccessTokenAuthorizable {
var baseURL: URL {
return URL(string: "https://example.com/api/v2")!
}
var path: String {
switch self {
case .setAuthentication(let security_code, let auth_token):
let userPasswordData = "*****".data(using: .utf8)
let base64EncodedCredential = userPasswordData!.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
let authString = "Basic \(base64EncodedCredential)"
print("basic \(authString)")
return [
"Authorization" : authString,
"Auth-Token":"\(auth_token)",
"Auth-Method" : "pwd"
]
}
}
var authorizationType: AuthorizationType? {
switch self {
case .setAuthentication(_, _):
return .basic
}
}
}
Here is network Manager :
protocol Networkable {
var provider: MoyaProvider<ServiceAPI> { get }
func setAuthentication(authToken:String,security_code:String,completion:@escaping (Data?)->Void)
}
class NetworkManager:Networkable {
static let shared = NetworkManager()
private init() {}
var provider = MoyaProvider<ServiceAPI>()
func setAuthentication(authToken: String, security_code: String, completion: @escaping (Data?) -> Void) {
provider.request(.setAuthentication(security_code: security_code, auth_token: authToken)) { (result) in
self.requestCompletion(result: result) { (data) in
completion(data)
}
}
}
//MARK: For The Completion
func requestCompletion(result:Result<Moya.Response, MoyaError>,completion:@escaping (Data?)->Void){
switch result {
case let .success(response):
do {
let successResponse = try response.filterSuccessfulStatusCodes()
completion(successResponse.data)
} catch let err {
print(err.localizedDescription)
completion(nil)
}
case let .failure(error):
print(error.localizedDescription)
completion(nil)
}
}
}
And here is setting password i m getting response too :
NetworkManager.shared.setAuthentication(authToken: auth, security_code: text) { (data) in
guard let data = data else {return}
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
print("Json \(json)")
} catch let error {
print(error)
}
}
When i am going to sign in i get incorrect password but i m setting same password and in backed also same password. how can i solve this issue ?
Hey, your authorization and stuff I believe must be in the headers. The path, for me, is ideally for the path itself. And if you''re passing params, do it in the task.
i have solve it
@ekramramu how you managed to solve it? what did you changed from your original implementation?
@ekramramu can you please specify how you were able to solve this?