10

I'm try to test the api I wrote with Fastapi. I have the following method in my router :

@app.get('/webrecord/check_if_object_exist')
async def check_if_object_exist(payload: WebRecord) -> bool:
    key = get_key_of_obj(payload.data) if payload.key is None else payload.key
    return await check_if_key_exist(key)

and the following test in my test file :

client = TestClient(app)
class ServiceTest(unittest.TestCase):
.....
    def test_check_if_object_is_exist(self):
        webrecord_json = {'a':1}
        response = client.get("/webrecord/check_if_object_exist", json=webrecord_json)
        assert response.status_code == 200
        assert response.json(), "webrecord should already be in db, expected : True, got : {}".format(response.json())

When I run the code in debug I realized that the break points inside the get method aren't reached. When I changed the type of the request to post everything worked fine.

What am I doing wrong?

8
  • Probably you are POSTing some data to an URL that expects GET requests. Fastapi uses starlette, which uses requests. I don't see any json parameter in get requests. Have you checked that the requeste url is correct? requests.readthedocs.io/en/master/user/quickstart/… Commented Apr 23, 2020 at 13:24
  • As u can see the request type is get and I'm using the get function of the client. The request url is indeed correct. How can I pass json in body to get request ? Commented Apr 23, 2020 at 15:25
  • That's my point. In order to pass data in a get request, you should encode it in the url and thus I asked about the url. I would change your type of request into POST, both in the test and the server code (so the server accepts POST requests for that url) Commented Apr 23, 2020 at 15:57
  • As I mentioned in the post that exactly what I have done aftewards and everything worked. I checked also the put method and its also based on request.put. There is an option to send there json , just need to pass on headers but it still doesnt work , check this post - stackoverflow.com/questions/38752091/… Commented Apr 23, 2020 at 16:02
  • That's my point. In order to pass data, you have to use POST, or as alternative, PUT. But GET cannot be used to pass data, unless you explicitly encode the data in the url, though it is not advisable if you have a lot of data or if you want it in a particular format (e.g. JSON). Either you change the representation of your data or you will never be able to pass data via the body with GET Commented Apr 23, 2020 at 16:07

1 Answer 1

6

In order to send data to the server via a GET request, you'll have to encode it in the url, as GET does not have any body. This is not advisable if you need a particular format (e.g. JSON), since you'll have to parse the url, decode the parameters and convert them into JSON.

Alternatively, you may POST a search request to your server. A POST request allows a body which may be of different formats (including JSON).

If you still want GET request

    @app.get('/webrecord/check_if_object_exist/{key}')
async def check_if_object_exist(key: str, data: str) -> bool:
    key = get_key_of_obj(payload.data) if payload.key is None else payload.key
    return await check_if_key_exist(key)


client = TestClient(app)
class ServiceTest(unittest.TestCase):
.....
    def test_check_if_object_is_exist(self):
        response = client.get("/webrecord/check_if_object_exist/key", params={"data": "my_data")
        assert response.status_code == 200
        assert response.json(), "webrecord should already be in db, expected : True, got : {}".format(response.json())

This will allow to GET requests from url mydomain.com/webrecord/check_if_object_exist/{the key of the object}.

One final note: I made all the parameters mandatory. You may change them by declaring to be None by default. See fastapi Docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.