Search code examples
iosswifthttphttpsplivo

HTTP request taking default method GET instead of POST


I'm trying to send sms through Plivo SMS API. Unfortunately, even though the request HTTP method is 'POST', the request posted as 'GET'. Please see my code below.

    let fromNumber = "11111111111"
    let toNumber = "111111234"
    let message = "Hello"

    do {
    let json = ["src":"\(fromNumber)","dst":"\(toNumber)","text":"\(message)"]
    let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted)
        print(jsonData)

    // Build the request
    let request = NSMutableURLRequest(URL: NSURL(string:"https://"\(authId)":"\(authToken)"@api.plivo.com/v1/Account/"\(authId)"/Message")!)

  // I'm assigning the method should be 'POST' but why its going as 'GET'

    request.HTTPMethod = "POST"  
    request.HTTPBody = jsonData

    // Build the completion block and send the request
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
            if error != nil{
                print("Error -> \(error)")
                return
            }

            do {
                let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]

                print("Result -> \(result)")

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

        task.resume()
        //return task



    } catch {
        print(error)
    }
}

Please look at the screenshot, the request posted as 'GET' request.Please help me to resolve this isuue.enter image description here


Solution

  • I kind of figured out what was the mistake. I should have put Message/ in the url.

    Before: NSURL(string:"https://"(authId)":"\ (authToken)"@api.plivo.com/v1/Account/"(authId)"/Message")

    Correct One: NSURL(string:"https://"(authId)":"\ (authToken)"@api.plivo.com/v1/Account/"(authId)"/Message/")

    Without "/" in the end, request posted as "GET" instead of "POST" Hope it helps others.