web-dev-qa-db-ja.com

Swiftを介してPOSTリクエストを送信するには?

このようなコントローラーがあります-

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 

Postmanを使用してこの要求を行うとき、Bodyを選択し、データを作成して、電子メールとパスワードを追加します。そしてこの作品

enter image description here

Swiftを使用して同じことをする方法は?これは私が試したことです

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

URLではなく、リクエストをsession.dataTaskに渡す必要があると思います

コードは次のようになります。

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

URL、パラメーターを送信できるカスタムHTTPクラスを作成し、APIからデータを取得します。以下はクラスです。

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()

  }

}

これで、以下のコードを参照して、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

POSTの例は、Eメールとパスワードの値をそれぞれ保持する2つの文字列としてuserEmailIDとUserpasswordでパラメーター「emailaddress」と「password」を使用してLogin APIを呼び出すAPIです。

以下に示すように、View ControllerのどこでもこのPOST APIを呼び出すことができます:

self.postLoginCall(url: "Your post method url")例: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