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

新手求助:如何用Python发送application/x-www-form-urlencoded格式的POST请求?

How to Send the Specified POST Request in Python

Hey there! Since you're new to Python, let's walk through exactly how to send that POST request you shared. I'll cover two common methods: one using Python's built-in tools (no extra installs required) and another with the popular requests library (way simpler for beginners).

First, let's recap the key details from your request example:

  • Target URL: http://www.chilkatsoft.com/httpTest/cityCountry.asp
  • Request method: POST
  • Content-Type: application/x-www-form-urlencoded
  • Form data: city=Paris&country=France

This is the most straightforward method. First, you'll need to install the library if you haven't already:

pip install requests

Then, here's the code to send your request:

import requests

# Define the target URL
url = "http://www.chilkatsoft.com/httpTest/cityCountry.asp"

# Define your form data as a dictionary (requests will handle encoding it correctly)
form_data = {
    "city": "Paris",
    "country": "France"
}

# Send the POST request
response = requests.post(url, data=form_data)

# Check the response
print("Response Status Code:", response.status_code)
print("Response Content:", response.text)

What's happening here?

  • The data parameter automatically sets the Content-Type header to application/x-www-form-urlencoded and encodes your dictionary into the city=Paris&country=France format.
  • response.status_code tells you if the request succeeded (200 means success).
  • response.text gives you the raw content returned by the server.

Using Python's Built-in urllib Module

If you don't want to install any extra libraries, you can use Python's built-in urllib modules. The code is a bit more verbose, but it works out of the box:

import urllib.request
import urllib.parse

# Define the target URL
url = "http://www.chilkatsoft.com/httpTest/cityCountry.asp"

# Encode the form data into the required format
form_data = urllib.parse.urlencode({
    "city": "Paris",
    "country": "France"
}).encode("utf-8")

# Create the request object with the correct headers
request = urllib.request.Request(url, data=form_data, method="POST")
request.add_header("Content-Type", "application/x-www-form-urlencoded")

# Send the request and get the response
with urllib.request.urlopen(request) as response:
    print("Response Status Code:", response.getcode())
    print("Response Content:", response.read().decode("utf-8"))

What's happening here?

  • urllib.parse.urlencode() converts the dictionary into the city=Paris&country=France string, then we encode it to bytes with .encode("utf-8") (required for sending over HTTP).
  • We manually set the Content-Type header since urllib doesn't do this automatically.
  • urlopen() sends the request, and we read and decode the response to get human-readable text.

Either of these methods will send the exact POST request you provided. I'd recommend starting with requests since it's much easier to read and maintain as you learn more Python!

内容的提问来源于stack exchange,提问作者BeNice

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:10:46