Search code examples
iosswiftgetdigital-oceanflask-restful

API request times out ONLY if I pass parameters


I am trying to figure out why any time I pass valid json to my server for a GET request the connection times out. I do not have any problems passing json to my server in any other request type other than GET... I've tested the server-side code locally and the queries work as expected.

I want to fetch a specific user from my database and I need to pass in a username, so I send the username to the server.

The error I keep getting (If I don't send any paramaters to the server, and just return current_user, it works and I don't get this error)

 Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x604000454c40 {Error Domain=kCFErrorDomainCFNetwork Code=-1001

Here is the part of the API call code where I set the request type and values to send to the server, in case:

let request = NSMutableURLRequest(url: url as URL)
    request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.httpBody = jsonData
    request.httpMethod = "GET"

Server set up, just in case it matters:
Digitalocean droplet, Linux, Ubuntu 16.04, Nginx

EDIT/ UPDATE
If I change the method from a GET to a POST (and keep the server code exactly the same), the server sends the correct data back immediately.

The server side code for this is very short, so I really don't see how it can be timing out due to optimization:

user = UserModel.find_by_username(data['username'])

    if user:
        return {"response": [user.json()]}, 200
    return {"response": "user not found"}, 404

It really seems as if we can't send json via a GET method. It doesn't seem to work on both a simulator nor an actual device...I saw a similar SO post where someone commented exactly what I'm experiencing. Changing GET to POST was the fix....but it is a GET request, so why wouldn't this work?


Solution

  • GET-Method does not support http body. When you send your parameter as url encoded it will work.

    Example:

    http://www.example.de?username=abc