3

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?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
MattBlack
  • 3,616
  • 7
  • 32
  • 58
  • 1
    You shouldn't. Sure, `curl` let's you do that, but `URLSession` doesn't because the body is undefined in `GET` request. If you want to include a body to your request, do `POST`. See https://stackoverflow.com/a/983458/1271826 – Rob Feb 22 '18 at 20:20
  • Hi Rob, thanks for the reply, ordinarily I would but the API wont accept POST, is there a way this is achievable as I don't have access to the API source. – MattBlack Feb 22 '18 at 20:21
  • IIRC, `URLSession` simply won't do that. You should talk to the team who are responsible for the the web service and have them fix this. (Are you 100% sure that it won't accept `POST` requests? It is very strange design and I think you'd have to contort yourself to not accept `POST` for JSON body.) – Rob Feb 22 '18 at 20:27
  • Hi, thanks for that, I'll see if they will make a change. I get the following when I POST: "message":"Request method 'POST' notsupported","path":"/statistics"} – MattBlack Feb 22 '18 at 20:30
  • 1
    OK, so I guess it won't. ;( Hopefully the web service team can help you... – Rob Feb 22 '18 at 20:32
  • It is okay to have a body in a GET method. The official specs have changed to allow this. If only iOS did support this... When you have to send a lot of data e.g. to calculate the price of a cart you want the method to be GET, not POST (since there are not side effects) and you don't want to send the whole cart in the query string. – Simon Backx Feb 13 '19 at 15:11

1 Answers1

-1

GET requests don't have a body, if you are using a body in your GET request you are not respecting HTTP 1.1 Specs:

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

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

Community
  • 1
  • 1
devyhan93
  • 49
  • 5
  • Technically, it is simply undefined, not explicitly disallowed. The implementing server, can and should ignore the body of a GET request, but it does not preclude a server from implementing and supporting it otherwise. This is an area where REST and HTTP/1.1 clash, as the spec suggests POST is the appropriate method. So in this case you can choose which standard you want to violate. – Austen Hoogen May 18 '22 at 23:18