Understanding HTTP Request Parameter Formats and Content-Type
The article explains that HTTP request parameters can be sent as URL‑encoded query strings for GET requests or as various POST body formats—such as application/json, application/x-www-form-urlencoded, multipart/form-data, or text/xml—each requiring the correct Content‑Type header so the backend can parse the data correctly, with JavaScript utilities like FormData, URLSearchParams, or qs helping developers serialize the payload.
Background
When an interface cannot be called, developers often wonder whether the problem lies in the parameter type they passed. The two most common difficulties in front‑back integration are incorrect parameter format and cross‑origin issues.
This article focuses on the first problem: what kinds of parameter formats exist and how to use them.
Thinking
For a POST request, who determines the request parameter type? Two possible answers are presented: (1) the backend interface restricts the accepted data type, or (2) the frontend decides the request parameter type when sending the request.
1. HTTP request parameter issue
The HTTP message consists of the request line, headers, and the body. This article concentrates on the data format inside the request body.
2. Various request parameter forms
1) GET request URL parameters (Query String Parameters)
Parameters are appended to the URL after a “?” and are URL‑encoded.
When using this method, developers usually serialize the object parameters before sending the request. For example, an Axios GET request can place parameters directly in the URL.
2) POST request common encoding types
First: application/json
This Content‑Type tells the server that the request body contains a JSON string, which is convenient for sending complex structured data.
Second: application/x-www-form-urlencoded
This is the classic URL‑encoded form submission where key‑value pairs are encoded as key1=val1&key2=val2. The server can parse this format easily.
Example payload:
Third: multipart/form-data
Used for file uploads. The request body is split into multiple parts, each with its own Content‑Disposition header.
Demo request using FormData:
Fourth: text/xml
This Content‑Type is used when the payload is XML, often in SOAP‑like remote calls.
Answer
The root cause of the parameter problem is usually the Content‑Type header. The frontend decides the request parameter data type, but the backend must be able to parse it correctly.
If the backend reports a parameter format error, consider which Content‑Type is appropriate for the current scenario.
How to define different types of parameters?
Third‑party tools such as qs can serialize parameters. Native JavaScript utilities like new FormData() or URLSearchParams can also be used.
Example JavaScript object and its URL‑encoded representation:
const params = {
name: 'John Doe',
age: 30,
city: 'New York'
};After processing with URLSearchParams, it becomes: name=John%20Doe&age=30&city=New%20York Why does Content‑Type exist?
Different Content‑Types correspond to different encoding schemes. Specifying the charset ensures browsers render characters correctly, preventing issues with special symbols and guaranteeing consistent interpretation across platforms.
Ximalaya Technology Team
Official account of Ximalaya's technology team, sharing distilled technical experience and insights to grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
