采用默认方法 GET 而不是 POST 的 HTTP 请求
HTTP request taking default method GET instead of POST
我正在尝试通过 Plivo SMS 发送短信 API。不幸的是,尽管请求 HTTP 方法是 'POST',但请求发布为 'GET'。请在下面查看我的代码。
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帮我解决这个问题。
我有点想通了是什么错误。我应该把 Message/ 放在 url 中。
之前:NSURL(string:"https://"(authId)":"\ (authToken)"@api.plivo.com/v1/Account/"(authId)"/Message")
正确一个:NSURL(string:"https://"(authId)":"\ (authToken)"@api.plivo.com/v1/Account/"(authId)"/留言/")
最后没有“/”,请求发布为 "GET" 而不是 "POST"
希望对其他人有帮助。
我有点明白了,所以代码如下所示:
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:authToken@api.plivo.com/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()
我正在尝试通过 Plivo SMS 发送短信 API。不幸的是,尽管请求 HTTP 方法是 'POST',但请求发布为 'GET'。请在下面查看我的代码。
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帮我解决这个问题。
我有点想通了是什么错误。我应该把 Message/ 放在 url 中。
之前:NSURL(string:"https://"(authId)":"\ (authToken)"@api.plivo.com/v1/Account/"(authId)"/Message")
正确一个:NSURL(string:"https://"(authId)":"\ (authToken)"@api.plivo.com/v1/Account/"(authId)"/留言/")
最后没有“/”,请求发布为 "GET" 而不是 "POST" 希望对其他人有帮助。
我有点明白了,所以代码如下所示:
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:authToken@api.plivo.com/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()