You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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.

Using Postman to Build HTTP Requests

Postman makes visualizing and configuring requests straightforward—here's how to get it done:

Setting Content-Type and Accept Headers

  1. Open Postman and click the + icon to create a new request tab.
  2. Choose your desired request method (GET, POST, PUT, DELETE, etc.) from the dropdown, then enter your target API URL in the address bar.
  3. Switch to the Headers tab (right next to Params).
  4. Add the Content-Type header:
    • In the empty Key field, type Content-Type
    • In the corresponding Value field, enter the format of your request body. Common values include:
      • application/json for JSON payloads
      • application/x-www-form-urlencoded for form data
      • multipart/form-data for file uploads
  5. Add the Accept header:
    • In a new Key field, type Accept
    • In the Value field, specify the format you want the server to return. For example:
      • application/json to get JSON responses
      • text/html for HTML content
      • application/xml for XML data

Adding a Request Body (for POST/PUT/PATCH)

If your request requires a body (like a POST to create a resource):

  1. Switch to the Body tab.
  2. Select the radio button matching your Content-Type (e.g., raw then choose JSON from the dropdown if you set Content-Type: application/json).
  3. Paste or type your request body content (e.g., {"username": "jane_doe", "age": 30}).
  4. 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!

Using curl to Build HTTP Requests

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 GET for GET requests)
  • -H: Adds a request header—use one -H per 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:04:10