gpt4 book ai didi

ios - Swift POST 语句返回 500 状态码

转载 作者:搜寻专家 更新时间:2023-10-31 22:13:18 24 4
gpt4 key购买 nike

我正在尝试对服务器进行邮寄调用,但我收到了 500 状态代码。我试过发送一个序列化的 json 对象和一个带参数的编码字符串。我无权访问服务器,所以我不知道这是否是服务器的问题,但 GET 和 DELETE 调用有效。 POST 和 PUT 没有。这是代码:

    guard let urlComponents = NSURLComponents(string: url) else
{
print("error creating URL")
return (nil, nil)
}

for (key, value) in urlParameters
{
urlComponents.queryItems = [NSURLQueryItem(name:key, value:value)]
}

let urlRequest = NSMutableURLRequest(URL: urlComponents.URL!)
urlRequest.HTTPMethod = "POST"
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)

do
{
let jsonUser = try NSJSONSerialization.dataWithJSONObject(["firstname": "Bob", "lastname": "Johnson", "summary": "Hi"], options: [])
urlRequest.HTTPBody = jsonUser
}
catch
{
print("Error: cannot create JSON from user")
}
let task = session.dataTaskWithRequest(urlRequest)
{
(data, response, error) in
....

我也试过像这样设置 httpbody:

    let postString = "firstname=Jack&lastname=Daniels&summary=hello"

urlRequest.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

这是我得到的服务器响应:

response: <NSHTTPURLResponse: 0x7ff3137a3b70> { URL: https://somelink } { status code: 500, headers {
Connection = "keep-alive";
"Content-Length" = 1477;
"Content-Type" = "text/html; charset=utf-8";
Date = "Thu, 11 Aug 2016 00:42:52 GMT";
Server = "WEBrick/1.3.1 (Ruby/2.2.4/2015-12-16)";
Via = "1.1 vegur";
"X-Request-Id" = "4c414334-4412-48a4-a61f-c17658182bdf";
"X-Runtime" = "0.007021";
} }

这是我的 POST 请求还是服务器的问题?

最佳答案

问题似乎是响应的格式与服务器尝试读取它的方式不同。因此,如果其他人遇到此问题,请仔细检查您的服务器(在我的情况下,我必须向某人发送电子邮件并接收正确的格式)。

关于ios - Swift POST 语句返回 500 状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38903659/

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