I am following API documentation where the end point is expecting JSON body on a GET request. The curl example is below:
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\"}]}"
I am using the following code in swift but the server gives the following error:
{"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()
}
How do I include JSON in the body of a GET request?