Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of JSON File upload Operation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the "JSON file upload operation case analysis" related knowledge, the editor through the actual case to show you the operation process, the operation method is simple and fast, practical, I hope that this "JSON file upload operation case analysis" article can help you solve the problem.

Think about the actions you want to express.

A common mistake is to focus on the specific file format required for the operation. Instead, we should consider the operation we want to express. The file format determines only the type of media used for the operation.

For example, suppose we want to design an API that allows users to upload profile pictures to their user accounts.

Here, it's usually a good idea to separate the avatar from the user account resources for a variety of reasons:

The avatar image is unlikely to change, so it may be a good candidate for caching. On the other hand, user account resources may contain content that changes frequently, such as the last login date.

Not all clients that access user accounts may be interested in avatars. Therefore, bandwidth can be saved.

For clients, it is usually best to load images separately (think about using the

Web application for tags)

You can access user account resources in the following ways:

/ users/

We can come up with a simple subresource that represents the avatar image:

/ users//avatar

The upload header looks like a simple replacement operation, which can be indicated by PUT:

PUT / users//avatarContent-Type: image/jpeg

If the user wants to delete his avatar, we can use a simple DELETE operation:

DELETE / users//avatar

Of course, customers need a way to display their avatars. Therefore, we can use GET to provide download operations:

GET / users//avatar

Return

HTTP/1.1 200 OkContent-Type: image/jpeg

In this simple example, we use a new subresource with common update, delete, and fetch operations. The only difference is that we use the image media type instead of JSON or XML.

Let's look at a different example.

Suppose we provide an API to manage the product data. We want to extend this API with an option to import the product from the uploaded CSV file. We should consider a way to express product import operations rather than file uploads.

Perhaps the easiest way is to send the POST request to a separate resource:

POST / product-importContent-Type: text/csv

Or we can think of it as a batch operation of the product. The PATCH method is a possible way to express batch operations on collections. In this case, the CSV document describes the expected changes to the product collection.

For example:

PATCH / productsContent-Type: text/csv action,id,name,pricecreate,Cool Gadget,3.99create,Nice cap,9.50delete,42

This example creates two new products and removes products with an id of 42.

It can take a long time to process file uploads. So consider designing it as an asynchronous REST operation.

Mix files and metadata

In some cases, we may need to attach additional metadata to the file. For example, suppose we have an API where users can upload holiday photos. In addition to the actual image data, the photo may also contain a description, location, and so on.

Here, I would recommend two separate operations for reasons similar to those for avatar images in the previous section. Even if the situation here is a little different (the data is linked directly to the image), it is usually a simpler approach.

In this case, we can first create a photo resource by sending the actual image:

POST / photosContent-Type: image/jpeg

In response, we got:

HTTP/1.1 201 CreatedLocation: / photos/123

After that, we can attach additional metadata to the photo:

Of course, we can also design it the other way around and send metadata before the image.

Embed Base64-encoded files in JSON or XML

If we cannot split the file content and metadata in a separate request, we can embed the file in the JSON/XML document using Base64 encoding. Using Base64 encoding, we can convert a binary format to a text representation that can be integrated into other text-based formats, such as JSON or XML.

A sample request might look like this:

POST / photosContent-Type: application/json {"width": "1280", "height": "920s", "filename": "funny-cat.jpg", "image": "TmljZSBleGFt...cGxlIHRleHQ="} mix media types with multipart requests

Another possible way to transmit image data and metadata in a single request / response is a multi-part media type.

Multi-part media types require a boundary parameter to be used as a separator between different body parts. The following request consists of two body parts. The first contains the image, while the second contains metadata.

For example:

POST / photosContent-Type: multipart/mixed; boundary=foobar-- foobarContent-Type: image/jpeg-- foobarContent-Type: application/json {"width": "1280", "height": "920", "filename": "funny-cat.jpg"}-- foobar--

Unfortunately, multipart requests / responses are often difficult to handle. For example, not every REST client can build these requests, and it is difficult to verify the response in a unit test.

This is the end of the content of "JSON file upload operation example analysis". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report