如何用Curl和Postman构造HTTP请求并配置参数获取响应
Hey there! Let's break down how to craft HTTP requests with both Postman and curl, including setting those crucial Content-Type and Accept headers. I'll walk you through step-by-step with practical examples to make it easy to follow.
Postman makes visualizing and configuring requests straightforward—here's how to get it done:
Setting Content-Type and Accept Headers
- Open Postman and click the + icon to create a new request tab.
- Choose your desired request method (GET, POST, PUT, DELETE, etc.) from the dropdown, then enter your target API URL in the address bar.
- Switch to the Headers tab (right next to Params).
- Add the
Content-Typeheader:- In the empty
Keyfield, typeContent-Type - In the corresponding
Valuefield, enter the format of your request body. Common values include:application/jsonfor JSON payloadsapplication/x-www-form-urlencodedfor form datamultipart/form-datafor file uploads
- In the empty
- Add the
Acceptheader:- In a new
Keyfield, typeAccept - In the
Valuefield, specify the format you want the server to return. For example:application/jsonto get JSON responsestext/htmlfor HTML contentapplication/xmlfor XML data
- In a new
Adding a Request Body (for POST/PUT/PATCH)
If your request requires a body (like a POST to create a resource):
- Switch to the Body tab.
- Select the radio button matching your
Content-Type(e.g.,rawthen chooseJSONfrom the dropdown if you setContent-Type: application/json). - Paste or type your request body content (e.g.,
{"username": "jane_doe", "age": 30}). - Hit the Send button to fire off the request and view the response in the lower pane.
Pro tip: Save your request (or a whole collection of requests) so you can reuse these header configurations later without retyping everything!
curl is a powerful command-line tool for making HTTP requests—here's how to replicate the same configurations:
Basic GET Request with Accept Header
To request data in a specific format, add the Accept header with the -H flag:
curl -H "Accept: application/json" https://api.example.com/users/123
POST Request with Content-Type, Accept, and Request Body
For a POST request sending JSON data, combine headers and the request body with -d:
curl -X POST \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ -d '{"username": "jane_doe", "age": 30}' \ https://api.example.com/users
Let's break down the flags:
-X POST: Specifies we're using the POST method (GET is default, so you can omit-X GETfor GET requests)-H: Adds a request header—use one-Hper header-d: Sends the request body data (for POST/PUT/PATCH)
For Form Data Requests
If you're sending form-encoded data instead of JSON, adjust the Content-Type and -d value:
curl -X POST \ -H "Content-Type: application/x-www-form-urlencoded" \ -H "Accept: application/json" \ -d "username=jane_doe&age=30" \ https://api.example.com/users
For File Uploads
To upload a file (using multipart/form-data), use the -F flag—curl will automatically set the correct Content-Type header, but you can still add Accept if needed:
curl -X POST \ -H "Accept: application/json" \ -F "profile_pic=@/path/to/your/image.jpg" \ -F "username=jane_doe" \ https://api.example.com/users/profile
内容的提问来源于stack exchange,提问作者Gorib Developer

