[GH-ISSUE #60] How to send custom POST request #39

Closed
opened 2026-03-03 16:45:02 +03:00 by kerem · 1 comment
Owner

Originally created by @alikazim on GitHub (May 7, 2015).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/60

I want to send my custom post request using your library. here is my code
Please help, how can I send my request using this library

    func callRequestAPI(url:String){

    var request = NSMutableURLRequest(URL: NSURL(string: url)!)
    var session = NSURLSession.sharedSession()
    request.HTTPMethod = "POST"

    var params:[String: AnyObject] = [
        "product_id" : selectedUberProductId,
        "start_latitude" : start_lat,
        "start_longitude" : start_lng,
        "end_latitude" : end_lat,
        "end_longitude" : end_lng]

     request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")

    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.allZeros, error: &err)

            let task = session.dataTaskWithRequest(request) {
                data, response, error in

                if let httpResponse = response as? NSHTTPURLResponse {
                    if httpResponse.statusCode != 202 {
                        println("response was not 202: \(response)")

                        return
                    }
                }
                if (error != nil) {
                    println("error submitting request: \(error)")
                    return
                }

                // handle the data of the successful response here
                var result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: nil) as! NSDictionary


                println(result)

                if let request_id: String = result["request_id"] as? String{

                    println(request_id)
                }

                if let driver: String = result["driver"] as? String{

                    println(driver)
                }

                if let eta: Int = result["eta"] as? Int{

                    println(eta)
                }

                if let location: String = result["location"] as? String{

                    println(location)
                }


                if let status: String = result["status"] as? String{

                    println(status)
                }


                if let surge_multiplier: Int = result["surge_multiplier"] as? Int{

                    println(surge_multiplier)
                }

                if let vehicle: String = result["vehicle"] as? String{

                    println(vehicle)
                }

            }

            task.resume()

     }
Originally created by @alikazim on GitHub (May 7, 2015). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/60 I want to send my custom post request using your library. here is my code Please help, how can I send my request using this library ``` func callRequestAPI(url:String){ var request = NSMutableURLRequest(URL: NSURL(string: url)!) var session = NSURLSession.sharedSession() request.HTTPMethod = "POST" var params:[String: AnyObject] = [ "product_id" : selectedUberProductId, "start_latitude" : start_lat, "start_longitude" : start_lng, "end_latitude" : end_lat, "end_longitude" : end_lng] request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") var err: NSError? request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.allZeros, error: &err) let task = session.dataTaskWithRequest(request) { data, response, error in if let httpResponse = response as? NSHTTPURLResponse { if httpResponse.statusCode != 202 { println("response was not 202: \(response)") return } } if (error != nil) { println("error submitting request: \(error)") return } // handle the data of the successful response here var result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: nil) as! NSDictionary println(result) if let request_id: String = result["request_id"] as? String{ println(request_id) } if let driver: String = result["driver"] as? String{ println(driver) } if let eta: Int = result["eta"] as? Int{ println(eta) } if let location: String = result["location"] as? String{ println(location) } if let status: String = result["status"] as? String{ println(status) } if let surge_multiplier: Int = result["surge_multiplier"] as? Int{ println(surge_multiplier) } if let vehicle: String = result["vehicle"] as? String{ println(vehicle) } } task.resume() } ```
kerem closed this issue 2026-03-03 16:45:03 +03:00
Author
Owner

@phimage commented on GitHub (Aug 7, 2015):

@alikazim could you explain why you can use current post method from OAuthSwiftClient
myoauthswift.client.post(

anyway with a custom NSMutableURLRequest you must take information from client after connection and add to the headers

if myoauthswift.client.credential.oauth2 {
    myrequest.headers = ["Authorization": "Bearer \(myoauthswift.client.credential.oauth_token)"]
} else {
   myrequest.headers = ["Authorization": OAuthSwiftClient.authorizationHeaderForMethod("POST", url: url, parameters: parameters, credential: myoauthswift.client.credential)]
 }

please close the issue if that answer your question

<!-- gh-comment-id:128834992 --> @phimage commented on GitHub (Aug 7, 2015): @alikazim could you explain why you can use current post method from `OAuthSwiftClient` `myoauthswift.client.post(` anyway with a custom `NSMutableURLRequest` you must take information from client after connection and add to the headers ``` swift if myoauthswift.client.credential.oauth2 { myrequest.headers = ["Authorization": "Bearer \(myoauthswift.client.credential.oauth_token)"] } else { myrequest.headers = ["Authorization": OAuthSwiftClient.authorizationHeaderForMethod("POST", url: url, parameters: parameters, credential: myoauthswift.client.credential)] } ``` please close the issue if that answer your question
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#39
No description provided.