求对应Python Requests代码的等效curl命令(附代码示例)
Hey there! Let's work out the correct curl command that matches your Python Requests code.
First, let's recap your original Python code:
import requests response = requests.request("POST" , url = "https://services27.ieee.org/fellowsdirectory/getpageresultsdesk.html" , data = { 'pageNum': 3 } ) print(response.text)
The key thing here is that when you use data with a dictionary in Requests, it sends the payload as form-encoded data (Content-Type: application/x-www-form-urlencoded), not JSON. Your initial curl attempt was sending JSON, which is why it wasn't matching the Python request's behavior.
Correct Equivalent curl Command
curl -X POST -d 'pageNum=3' https://services27.ieee.org/fellowsdirectory/getpageresultsdesk.html
Alternatively, if you want explicit URL encoding (useful if your values have special characters), you can use:
curl -X POST --data-urlencode 'pageNum=3' https://services27.ieee.org/fellowsdirectory/getpageresultsdesk.html
Quick Explanation
- Your Python code uses
data={'pageNum': 3}: This tells Requests to encode the key-value pair like an HTML form would, setting the appropriate Content-Type header automatically. - The curl
-dflag does exactly this—it sends form-encoded data, matching what your Python code is doing. Your original attempt used JSON syntax ({"pageNum": "3"}), which would set the Content-Type toapplication/json, which isn't what the Python request is sending.
If you want to double-check that the requests match, add the -v flag to the curl command to view the full request headers and payload, which should align with what Requests generates.
内容的提问来源于stack exchange,提问作者user1424739

