From the course: Build REST APIs with FastAPI

Unlock this course with a free trial

Join today to access over 24,200 courses taught by industry experts.

Raw data

Raw data

In some cases, we want to work directly with the raw request that the user sent us. For example, here's an HTTP server that gets an image and returns the dimension of the image, the length, and the width. So I'm importing request also from FastAPI. And I'm using the PIL imaging library to work with the image. And I'm defining a maximal image size of five megabytes. It's very important not to blindly accept everything that is being sent to you from the internet. And it's saying, this is the size. So this is /size, and it accepts a request. And this is telling FastAPI that you are going to get the raw request. Note that this is an async handler, and this is because when we're dealing with getting the full body, it must be an async handler. And we're going to have to do an await. So first thing, we just accessing the headers. So request.headers. get and we get the content length. This is how many bytes the client is sending. And if you don't have a size or if it's bigger than the maximum…

Contents