How to pass access token to Alamofire?

前端 未结 4 450
南旧
南旧 2020-12-28 19:19

I am trying to pass access token in Alamofire but getting confuse in various methods around web.

Below are methods which we need to use.



        
4条回答
  •  孤独总比滥情好
    2020-12-28 19:55

    It can be done by using Alamofire in following way:

        let url: String = "https:url......."
        var request = URLRequest(url:  NSURL(string: url)! as URL)
    
        // Your request method type Get or post etc according to your requirement
        request.httpMethod = "POST"
    
        request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    
        // Your required parameter in case of Post request
        let parameters: [String: Any] = ["paramter1": "vaue1"]
    
        request.httpBody = try! JSONSerialization.data(withJSONObject: parameters )
    
        Alamofire.request(request).responseJSON { (responseObject) -> Void in
    
          // Your required functionality here            
    
        }
    

提交回复
热议问题