gpt4 book ai didi

ios - HTTP 请求采用默认方法 GET 而不是 POST

转载 作者:行者123 更新时间:2023-11-29 00:59:22 25 4
gpt4 key购买 nike

我正在尝试通过 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”请求。请帮助我解决此问题。 enter image description here

最佳答案

我有点想通了是什么错误。我应该把 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)"/Message/")

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

关于ios - HTTP 请求采用默认方法 GET 而不是 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37199167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com