gpt4 book ai didi

arrays - JSON 语法 - 正文发布请求 - SWIFT4

转载 作者:行者123 更新时间:2023-11-30 10:52:05 25 4
gpt4 key购买 nike

我正在尝试使用来自服务器的 HTTP Post 请求获取 JSON,但我认为我的请求正文是错误的。我尝试了一切,但找不到解决方案。应用程序崩溃了。

我在请求正文(newTodo 变量)上尝试了一些不同的语法,例如:

'{"api_key":"mykey123","api_secret":"asdfg","uniqueid":"csd23cdse34ww","password":"secret123","pin":"12345"}'

["api_key": "mykey123", "api_secret": "asdfg","uniqueid": "csd23cdse34ww","password": "secret123","pin": "12345"]

使用上面的方法出现错误:

["api_key": "mykey123", "password": "flibble1", "uniqueid": "csd23cdse34ww", "api_secret": "secret123", "pin": "12345"] 2019-01-25 10:00:54.298086+0000 APPTEST[8863:646933] [logging] table "users" already exists table "users" already exists (code: 1) .
error parsing response from POST on /todos

在 Postman 的 body 上效果很好

'{ "api_key": "mykey123", "api_secret": "asdfg", "uniqueid": "csd23cdse34ww", "password": "secret123", "pin": "12345", }'

但是 Xcode 要求我使用下面的语法。

func loginPressed() {
let todosEndpoint: String = "https://mydevapi.com/authenticateuser"
guard let todosURL = URL(string: todosEndpoint) else {
print("Error: cannot create URL")
return
}
var todosUrlRequest = URLRequest(url: todosURL)
todosUrlRequest.httpMethod = "POST"

let newTodo = "{\"api_key\":\"mykey123\",\"api_secret\":\"asdfg\",\"uniqueid\":\"csd23cdse34ww\",\"password\":\"secret123\",\"pin\":\"12345\"}"

let jsonTodo: Data
do {
jsonTodo = try JSONSerialization.data(withJSONObject: newTodo, options: [])
todosUrlRequest.httpBody = jsonTodo
print(newTodo)
} catch {
print("Error: cannot create JSON from todo")
return
}

let session = URLSession.shared

let task = session.dataTask(with: todosUrlRequest) {
(data, response, error) in
guard error == nil else {
print("error calling POST on /todos/1")
print(error!)
return
}
guard let responseData = data else {
print("Error: did not receive data")
return
}

// parse the result as JSON, since that's what the API provides
do {
guard let receivedTodo = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: Any] else {
print("Could not get JSON from responseData as dictionary")
return
}
print(receivedTodo)
print("The todo is: " + receivedTodo.description)

guard let todoID = receivedTodo["id"] as? Int else {
print("Could not get todoID as int from JSON")
print(receivedTodo)
return
}
print("The ID is: \(todoID)")
print(receivedTodo)
} catch {
print("error parsing response from POST on /todos")
return
}
}
task.resume()
}

应用程序崩溃 - 线程 1:发出 SIGABRT 信号

2019-01-25 09:37:05.762339+0000 APPTEST[8601:615340] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* +[NSJSONSerialization dataWithJSONObject:options:错误:]:JSON 写入中的顶级类型无效'*** 首先抛出调用堆栈:( 0 CoreFoundation 0x00000001087f11bb 异常预处理 + 331 1 libobjc.A.dylib 0x000000010635f735 objc_异常_抛出 + 48 2 CoreFoundation 0x00000001087f1015 +[NSException 引发:格式:] + 197 3 基础 0x0000000105eb2dd5 +[NSJSONSerialization dataWithJSONObject:选项:错误:] + 253 4 APPTEST 0x000000010588ab5e $S9Bibimoney15LoginControllerC12loginPressedyyF + 2718 5 应用测试 0x000000010588f79c $STA.7 + 28 6 APPTEST 0x00000001058924d4 $S9Bibimoney9LoginViewC06handleB0yyF + 132 7 APPTEST 0x0000000105892534 $S9Bibimoney9LoginViewC06handleB0yyFTo + 36 8 UIKitCore 0x000000010c876ecb -[UIApplication sendAction:to:from:forEvent:] + 83 9 UIKitCore 0x000000010c2b20bd -[UIControl sendAction:to:forEvent:] + 67 10 UIKitCore 0x000000010c2b23da -[UIControl _sendActionsForEvents:withEvent:] + 450 11 UIKitCore 0x000000010c2b131e-[UIControl TouchsEnded:withEvent:] + 583 12 UIKitCore 0x000000010c8b20a4 - [UIWindow _sendTouchesForEvent:] + 2729 13 UIKitCore 0x000000010c8b37a0 - [UIWindow发送事件:] + 4080 14 UIKitCore 0x000000010c891394 - [UIApplication发送事件:] + 352 15 UIKitCore 0x000000010c9665a9 __dispatchPreprocessedEventFromEventQueue + 3054 16 UIKitCore 0x000000010c9691cb __handleEventQueueInternal + 5948 17 核心基础 0x0000000108756721 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 18 核心基础 0x0000000108755f93 __CFRunLoopDoSources0 + 243 19 核心基础 0x000000010875063f __CFRunLoopRun + 1263 20 核心基础 0x000000010874fe11 CFRunLoopRunSpecific + 625 21 图形服务 0x000000011047c1dd GSEventRunModal + 62 22 UIKitCore 0x000000010c87581d UIApplicationMain + 140 23 应用测试 0x0000000105896517 主 + 71 24 libdyld.dylib 0x0000000108d93575 开始 + 1 25 ??? 0x0000000000000001 0x0 + 1)libc++abi.dylib:以 NSException 类型的未捕获异常终止(lldb)

  • 预期状态代码 200

最佳答案

我很确定您没有发送您所期望的内容。看来您尝试对“已经是”JSON 的字符串进行 JSON 编码。您应该打印出 jsonTodo 的内容(你会惊讶地发现你会看到如此多的逃脱……)。 JSONSerialization如果您将 Dictionary 传递给它,可能会做正确的事情,但通过 JSONDecoder 执行此类转换要快捷得多和 Codeable协议(protocol)。

您应该将问题简化为手头的实际问题,以便人们提供帮助。尝试集中精力在 Data 中创建正确的 JSON对象,一旦你明白了,事情就会水到渠成。

为了获得当前问题的具体答案,您应该使用jsonTodo的内容修改您的问题。 .

关于arrays - JSON 语法 - 正文发布请求 - SWIFT4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54351924/

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