gpt4 book ai didi

json - 带有 json 正文的 Swift GET 请求

转载 作者:行者123 更新时间:2023-12-05 08:08:29 25 4
gpt4 key购买 nike

我正在关注 API 文档,其中端点期望 GET 请求中的 JSON 正文。 curl 示例如下:

curl -i -b /cookie.txt -H "Content-Type: application/json" -X GET -d "{\"groups\": [{\"groupId\":\"1\"}],\"originatorId\": \"2\",\"schedule\": \"1234564334\",\"shortenedLink\": \"http://\",\"linkMessage\": \"Click on this to be directed to\",\"messageTips\": [{\"date\": \"12/12/2018\",\"time\": \"16:00\",\"venue\": \"chelt\",\"odds\": \"3/1\",\"betEntityName\": \"Dobbin\",\"bet\": \"10\",\"messageText\": \"Lorem ipsum dolor sit amet, consectetur adipiscing elit\"}, {\"date\": \"12/12/2018\",\"time\": \"17:00\",\"venue\": \"york\",\"odds\": \"3/1\",\"betEntityName\": \"Bobbin\",\"bet\": \"20\",\"messageText\": \"Lorem ipsum dolor sit amet, consectetur adipiscing elit\"}]}"

我在 swift 中使用了以下代码,但服务器给出了以下错误:

{"timestamp":1519328960600,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing: public org.springframework.http.ResponseEntity

func test() {
let requestUrl: URL = URL(string: "\(serverEP)/statistics")!

var urlRequest = URLRequest(url: requestUrl)
urlRequest.httpMethod = "GET"
urlRequest.setValue("ID=\(defaults.string(forKey: "cookie")!)", forHTTPHeaderField: "Cookie")
urlRequest.httpShouldHandleCookies = true
urlRequest.timeoutInterval = 3

let postString = "{\"groups\":\"test\"}"

urlRequest.httpBody = postString.data(using: .utf8)

let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
if error == nil {
if let data = data,
let html = String(data: data, encoding: String.Encoding.utf8) {
print(html)
}
}
}
task.resume()
}

如何在 GET 请求的正文中包含 JSON?

最佳答案

GET 请求没有正文,如果您在 GET 请求中使用正文,则您不遵守 HTTP 1.1 规范:

https://www.rfc-editor.org/rfc/rfc2616#section-4.3

https://www.rfc-editor.org/rfc/rfc2616#section-9.3

关于json - 带有 json 正文的 Swift GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48936426/

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