使用Python requests库向NiFi发送数据失败,寻求技术协助
Let's work through this step by step to get your data flowing into NiFi. Here are the most common fixes and checks:
1. Fix Your Python Request First
Your current code serializes JSON but doesn't explicitly tell NiFi what type of data you're sending—this is likely the biggest blocker. NiFi's ListenHTTP relies on clear content type headers to properly parse incoming payloads.
Updated Code Option 1 (Recommended)
Use requests built-in json parameter, which automatically handles serialization and sets the correct Content-Type: application/json header for you:
import requests url = 'http://dev.test.com:7001/contentListener' payload = {'some': 'data'} r = requests.post(url, json=payload) # Add these lines to debug the response print(f"Status Code: {r.status_code}") print(f"Response Text: {r.text}")
Updated Code Option 2 (Manual Header Approach)
If you prefer to keep using json.dumps(), explicitly set the content type header to let NiFi know you're sending JSON:
import requests import json url = 'http://dev.test.com:7001/contentListener' payload = {'some': 'data'} headers = {'Content-Type': 'application/json'} r = requests.post(url, data=json.dumps(payload), headers=headers) # Debug output to see what NiFi sends back print(f"Status Code: {r.status_code}") print(f"Response Text: {r.text}")
The debug prints will immediately flag issues like 404 (wrong path), 405 (method not allowed), or 500 (server error)—these will point us straight to the next set of checks.
2. Verify NiFi ListenHTTP Configuration
Since you didn't share your component settings, here are the critical ones to double-check:
- Listening Port: Confirm it's set to
7001—mismatched ports are a super frequent culprit. - Context Path: In the
HTTP Context Mapconfiguration, make sure theContext Pathis exactly/contentListener(including the leading slash). If you leave this blank, your request URL should behttp://dev.test.com:7001instead. - Allowed HTTP Methods: Ensure
POSTis included in this field. By default, some NiFi versions only allowGET—addPOST(comma-separated, e.g.,GET,POST) to enable receiving POST requests. - Component State: Make sure the ListenHTTP processor is in the RUNNING state (not stopped or disabled). Check the NiFi UI for any error icons next to the component.
3. Debug & Validate in NiFi
To confirm if data is coming in (even if you can't see it yet):
- Add a
LogAttributeprocessor to your flow. - Connect the
successrelationship from ListenHTTP to LogAttribute. - Configure LogAttribute to log all attributes (set "Attributes to Log" to
*). - Run the flow and send your Python request—check the NiFi logs (or the LogAttribute's output) for incoming request details like headers, content length, and payload.
4. Network & Permissions Checks
- Confirm the machine running your Python script can reach the NiFi server: Try pinging
dev.test.comor usingtelnet dev.test.com 7001to verify the port is open. - Check NiFi's
nifi-app.log(usually in thenifi/logsdirectory) for errors like "path not found" or "method not allowed"—these will give direct clues about what's misconfigured.
Once you fix the content type header and verify the NiFi settings, your data should start flowing through!
内容的提问来源于stack exchange,提问作者amira khalifa

