2016-05-12 78 views
0

我试图通过Plivo SMS API发送短信。不幸的是,即使请求HTTP方法是'POST',请求也发布为'GET'。请参阅下面的代码。以默认方法GET取代POST的HTTP请求

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

请看截图,请求发布为'GET'request.Please帮我解决这个问题。 enter image description here

回答

0

我有点想通了什么是错误。我应该把消息/在网址中。

前:NSURL(字符串: “https://开头”(验证ID) “:” \(的authToken) “@ api.plivo.com/v1/Account/"(authId)"/Message”)

正确之一:NSURL(string:“https://”(authId)“:”\(authToken)“@ api.plivo.com/v1/Account/"(authId)"/0 Message/”)

最后没有“/”,请求发布为“GET”而不是“POST” 希望它可以帮助其他人。

0

我有点想通了,这样的代码如下所示:

 let json = ["src":"Source","dst":"Destination","text":"Test SMS"] 
     let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: []) 
     print(jsonData) 

     // Build the request 
     let request = NSMutableURLRequest(URL: NSURL(string:"https://authID:[email protected]/v1/Account/authID/Message/")!) 

     request.HTTPMethod = "POST" 
     request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") 
     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: []) 

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

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

     task.resume()