Alamofire Swift конвертировать - PullRequest
0 голосов
/ 26 апреля 2018

Привет! Я хочу сделать запрос к API, но при отправке консоль покажет мне

invalidURL

 Alamofire.request("https://.../api/v1.8/set/order/?address=\(address)&email=\(email)&information=\(information)&name=\(name)&order=\(parameters)&password=\(password)&paymentType=\(paymentType)&phone=\(phone)&token=\(token)&userID=\(userID)&wihtRegistration=\(wihtRegistration)").validate(statusCode: 200..<300)
        .responseJSON { response in
        switch response.result
        {
        case .failure(let error):
            print(error)

        case .success(let value):
             print(value)

        print("Request: \(response.request)")

    }
}

Как конвертировать в Alamofire?

Ответы [ 2 ]

0 голосов
/ 26 апреля 2018

Попробуйте это

 let jsonObject = [["code": 404, "counts": 15]]

    let json = JSON(jsonObject)

    // Generate the string representation of the JSON value
    let jsonString = json.rawString(.utf8)!
    let params = ["name": name, "phone": phone, "address": address, "information": information, "email": email, "userID":userID, "wihtRegistration": wihtRegistration, "password": password, "token": token, "paymentType": paymentType, "order" : jsonString] as [String : Any]

    Alamofire.request("http:...", method: .get, parameters: params)
        .responseString { response in
            #if DEBUG
(request!.url!.absoluteString)\n\(request!.httpBody.map { body in String(data: body, encoding: .utf8) ?? "" } ?? "")")
            switch response.result {
            case .success(let value):
                print("Response with content \(value)")
            case .failure(let error):
                print("Response with error: \(error as NSError): \(response.data ?? Data())")
            }
            #endif
    }
0 голосов
/ 26 апреля 2018

Я создал этот пользовательский метод. Вызвать это, передав необходимый параметр: -

Без кодировки JSON

func requestWithoutJSONEncoding(_ method: HTTPMethod
    , _ URLString: String
    , parameters: [String : AnyObject]? = [:]
    , headers: [String : String]? = [:]
    , completion:@escaping (Any?) -> Void
    , failure: @escaping (Error?) -> Void) {

    Alamofire.request(URLString, method: method, parameters: parameters, headers: headers)
        .responseJSON { response in

            switch response.result {
            case .success:
                completion(response.result.value!)
            case .failure(let error):
                failure(error)
            }
    }
}

с кодировкой JSON

func request(_ method: HTTPMethod
    , _ URLString: String
    , parameters: [String : AnyObject]? = [:]
    , headers: [String : String]? = [:]
    , completion:@escaping (Any?) -> Void
    , failure: @escaping (Error?) -> Void) {

    Alamofire.request(URLString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
        .responseJSON { response in

            switch response.result {
            case .success:
                completion(response.result.value!)
            case .failure(let error):
                failure(error)
            }
    }
}
...