Best Way of Design a File Upload Rest Api

PROGRAMMING Residuum APIS

Parse a raw file or a file in HTML form-data

Epitome past Girija Shankar Behera

In this article we will explore how to handle file uploads in Remainder API frameworks. Earlier I accept published a few articles related to REST APIs where nosotros accept covered all the Basics, and the Constraints to brand an API Restful, and then a couple of hands-on (Part 1, Part 2 and Role 3).

This time, we are going to run across something dissimilar. This does non really impact on the RestFul-ness of the API, but a useful characteristic to have while edifice an API system. i.east. handling of file uploads via an API request. This article is going to be short as there's not much to explore in information technology. I've used Django Balance Framework to develop the APIs and Postman to exam them.

Okay this is what the API looks like. Simple i, only a post method which accepts a request and returns the data in the request itself.

Left image. POST request with raw JSON information, Content-Blazon: awarding/json. Right prototype. Mail service request with raw file data, Content-Type: image/jpeg. Image by Girija Shankar Behera

Hit the above API, if nosotros send raw data in JSON format (with Content-Blazon header set to awarding/json), the output contains exactly the aforementioned data that we are sending in the request. But when we hitting the API with a file in the request (with Content-Blazon header fix to epitome/jpeg), the response is 'Unsupported Media blazon 'image/jpeg' in request'. So basically, API was able to understand the JSON data in the request, but non a file type of information.

Parsers

An API by Django Rest Framework has a default gear up of parsers that it uses to detect the type of the data in the request. The list of parsers in are JSONParser, FormParser, MultiPartParser, and FileUploadParser. JSONParser parses the JSON data in a request, FormParser parses the HTML form data in a request, MultiPartParser parses the HTML course data with file in a request, and FileUploadParser parses the file data in raw format. In this commodity, nosotros are going to explore the MultiPartParser and the FileUploadParser.

How Django RF knows which Parser to utilise?

Well, allow's take a await at the default listing of parsers that the API is using to understand the request type.

The default parsers here in our API are JSONParser, FormParser and MultiPartParser. API determines the parser to use by looking into the Content-Type field in the header of the request. i.e. The API looked into the Content-Type for the offset request, which was application/json and hence selected the JSONParser, whereas for the second request with Content-Blazon image/jpeg, the API had no parser in the default list available. That'due south why it threw the bulletin saying 'Unsupported media blazon'.

Well, in that location was a MultiPartParser available, but it did not solve our purpose. Allow'due south come across why.

MultiPartParser

This parser is used to handle file uploads in a HTML form. This is different from our case to a higher place, as in our case the asking had a raw file, non a HTML form.

sending a file in form-data in a POST request. Image by Girija Shankar Behera

The API above is debugged at runtime to see how MultiPartParser works. This parser is enabled past default, and so if nosotros hit the API with a file in a form-data, then the backend will be able to understand the content type in the asking. Hence, request.data produces the querydict with filename and its format in the output.

Well, if we are uploading a file from the web, then this is the manner to go. But what I was looking for was uploading a Raw file.

FileUploadParser

For a raw file, we demand to use FileUploadParser in our API. And as it's non there in the default set up of parsers, and nosotros need to tell our API manually to utilize this.

sending a file in raw information in a POST request, Content-Disposition: attachment; filename=healthy_leaves_1.png. Image by Girija Shankar Behera

In the code snippet, nosotros run across that the default parser is changed to a list containing just FileUploadParser now. And there was no error as well, as the API was able to sympathise the data in the request. Still I had to do i more update hither in the client side, i.east. at that place is no mode the API is going to go the name of the file, and then it won't be able to admission the file, unless we have a fashion of providing the name externally. So we add some other header for the file proper name similar this: 'Content-Disposition: attachment; filename=healthy_leaves_1.png'.

This made sure that the API gets to know the name of the file, and gets access to it from the asking.

Well, this only ensures that the API knows that the asking has a file blazon of data in information technology. Backend still needs to handle the specific file uploads depending on their types. Like in the asking above, we are sending an image file. To read this, we notwithstanding need to apply the Prototype libraries available in Python.

Handling of Image file format. Image by Girija Shankar Behera

Conclusion

Well, that's it for this commodity. This was again a role of the REST API series that I have published before. We explored 2 dissimilar means to handle file uploads in a REST API, MultiPartParser, which can be used to handle a file upload from the web, and FileUploadParser, which can be used to handle raw file uploads.

Thank yous.

References:

  • Parsers in Django Residual Framework

woodsandsorses.blogspot.com

Source: https://towardsdatascience.com/rest-api-file-upload-handling-851747c0c2b7

0 Response to "Best Way of Design a File Upload Rest Api"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel