web-dev-qa-db-fra.com

Comment envoyer une demande POST via Swift?

J'ai mon contrôleur comme ça -

def create
   if (@user = User.find_by_email(params[:email])) && @user.valid_password?(params[:password])
      render json: @user.as_json(only: [:email,:authentication_token]),status: :created
   else 
      render json:('Unauthorized Access')
   end  
end 

Lorsque j'utilise Postman pour faire cette demande, je choisis le corps et les données du formulaire et j'ajoute l'e-mail et le mot de passe. Et cela FONCTIONNE

enter image description here

Comment utiliser Swift pour faire de même? C'est ce que j'ai essayé

let url = URL(string: "http://localhost:3000/api/v1/user_serialized/")

let config = URLSessionConfiguration.default

let request = NSMutableURLRequest(url: url!)

request.httpMethod = "POST"

let bodyData = "[email protected]&password=Test1234"

request.httpBody = bodyData.data(using: String.Encoding.utf8);

let session = URLSession(configuration: config)

let task = session.dataTask(with: url! as URL, completionHandler: {(data, response, error) in
    let json = JSON(data:data!)

    debugPrint(json)
})

task.resume()
10
user2775042

Je pense que vous devriez transmettre votre demande au lieu de l'url à session.dataTask

voici à quoi ressemble mon code:

private let url = URL(string: "http://example.com/")!

func httpPost(jsonData: Data) {
    if !jsonData.isEmpty {
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.httpBody = jsonData

        URLSession.shared.getAllTasks { (openTasks: [URLSessionTask]) in
            NSLog("open tasks: \(openTasks)")
        }

        let task = URLSession.shared.dataTask(with: request, completionHandler: { (responseData: Data?, response: URLResponse?, error: Error?) in
            NSLog("\(response)")
        })
        task.resume()
    }
}
10
ndreisg

J'ai créé une classe HTTP personnalisée où nous pouvons envoyer des URL, des paramètres et nous obtiendrons des données de l'API. Voici la classe.

import Foundation

//HTTP Methods
enum HttpMethod : String {
   case  GET
   case  POST
   case  DELETE
   case  PUT
}


class HttpClientApi: NSObject{

//TODO: remove app transport security arbitary constant from info.plist file once we get API's
 var request : URLRequest?
 var session : URLSession?

static func instance() ->  HttpClientApi{

    return HttpClientApi()
}



func makeAPICall(url: String,params: Dictionary<String, Any>?, method: HttpMethod, success:@escaping ( Data? ,HTTPURLResponse?  , NSError? ) -> Void, failure: @escaping ( Data? ,HTTPURLResponse?  , NSError? )-> Void) {

     request = URLRequest(url: URL(string: url)!)

    logging.print("URL = \(url)")

    if let params = params {


        let  jsonData = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)

        request?.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request?.httpBody = jsonData//?.base64EncodedData()


        //paramString.data(using: String.Encoding.utf8)
    }
    request?.httpMethod = method.rawValue


    let configuration = URLSessionConfiguration.default

    configuration.timeoutIntervalForRequest = 30
    configuration.timeoutIntervalForResource = 30

    session = URLSession(configuration: configuration)
    //session?.configuration.timeoutIntervalForResource = 5
    //session?.configuration.timeoutIntervalForRequest = 5

    session?.dataTask(with: request! as URLRequest) { (data, response, error) -> Void in

        if let data = data {

            if let response = response as? HTTPURLResponse, 200...299 ~= response.statusCode {
                success(data , response , error as? NSError)
            } else {
                failure(data , response as? HTTPURLResponse, error as? NSError)
            }
        }else {

            failure(data , response as? HTTPURLResponse, error as? NSError)

        }
        }.resume()

  }

}

Vous pouvez maintenant vous référer au code ci-dessous pour savoir comment passer un appel API.

  var paramsDictionary = [String:Any]()

    paramsDictionary["username"] = "BBB"
    paramsDictionary["password"]    = "refef"

    HttpClientApi.instance().makeAPICall(url: "Your URL", params:paramsDictionary, method: .POST, success: { (data, response, error) in

        // API call is Successfull

    }, failure: { (data, response, error) in

        // API call Failure

    })
15
Balaji Galave

Voici l'exemple de l'API POST pour appeler l'API de connexion avec les paramètres "emailaddress" et "password" avec userEmailID et Userpassword comme deux chaînes contenant respectivement les valeurs de l'e-mail et du mot de passe.

Vous pouvez appeler cette POST n'importe où dans votre contrôleur de vue, comme indiqué ci-dessous:

self.postLoginCall (url: "Votre méthode de publication url") exemple: self.postLoginCall (url: " http: //1.0. 0,1/api/login.php ")

func postLoginCall(url : String){


    let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
    request.httpMethod = "POST"
    let postString = "emailaddress=\(userEmailID!)&password=\(Userpassword!)"
    print(postString)
    request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.httpBody = postString.data(using: String.Encoding.utf8)

    let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
        guard error == nil && data != nil else {                                                          // check for fundamental networking error
            print("error=\(error)")
            return
        }

        do {
        if let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [String:AnyObject]{
            print(responseJSON)
            print(responseJSON["status"]!)


            self.response1 = responseJSON["status"]! as! Int

            print(self.response1)

            //Check response from the sever
            if self.response1 == 200
            {
                OperationQueue.main.addOperation {

                    //API call Successful and can perform other operatios
                   print("Login Successful")
                }

            }

            else
            {
                OperationQueue.main.addOperation {

                    //API call failed and perform other operations
                    print("Login Failed")

                }

            }


        }
        }
        catch {
            print("Error -> \(error)")
        }



    }


    task.resume()



}
2
Rouny