[Android] The difference between the commonly used data forms in the interface Body request

What is the difference between body and params

In the interface request, the body and params mainly have the following differences:

The request location is different: body is usually used to send request parameters as part of the request body, while params is used to send request parameters as part of the URL. Generally speaking, body is used to pass large or sensitive data, while params is used to pass small or non-sensitive data.

The transmission methods are different: body transmits the request parameters in a certain format (such as JSON, XML, etc.) in the request body, while params concatenates the request parameters in the form of key-value pairs in the query string of the URL for transmission.

The security is different: since the parameters in the body are placed in the request body, it is safer because the parameters will not be exposed in the URL; while the parameters in params will be exposed in the URL, which may be intercepted by interceptors and proxies server or logging.

Applicable scenarios are different: body is suitable for situations where a large amount of data needs to be transmitted, file uploads, and special request body formats, such as POST requests and PUT requests. params is suitable for passing some simple query parameters, such as GET requests.

It should be noted that different interface designs may have different requirements on the position and delivery method of parameters, and which method to use depends on the interface document or the definition of the backend API. In common RESTful interface design, GET requests generally use params, while POST requests use body.

What is the difference between from-data and json and raw in the body request body

form-data, json, and raw are common request body parameter formats, and they have some differences in data encoding and structure in the request body:

form-data: form-data is a common MIME type used to transmit data in the form of key-value pairs in HTTP requests. It simulates the way HTML forms are submitted and encodes parameter data in the form of fields. Each field consists of a unique key and corresponding value. Can be used to upload files or pass content containing text data and binary data.

json: json is a text-based data exchange format that passes data in JSON format in the request body. The JSON format uses key-value pairs to represent data and has more flexible structure and data type support. Specify that the data in the request body is in JSON format by using the Content-Type: application/json request header.

raw: raw means raw data, which usually appears in the request body in the form of plain text. When using raw, you can choose from different encodings, such as plain text, XML, JSON, etc. You need to specify the correct Content-Type to ensure that the request body is parsed correctly.

The choice of which format to use depends on the requirements and design needs of the backend API:

If the API requires data to be passed in a form, then use form-data.
If the interface requires JSON format for data transfer, then use json.
If the interface has no special requirements on the format of the request body, or you need to customize the format of the request body, you can use raw and select an appropriate encoding method.

Guess you like

Origin blog.csdn.net/qq_43358469/article/details/131727869