Flask/Connexion+Tornado中返回204 No Content的报错解决求助
Got it, let's work through this problem together. You're stuck trying to return a proper 204 No Content response from your Connexion view (running on Tornado)—neither return None, 204 nor return '', 204 works because Flask rejects None outright, and an empty string triggers errors since 204 responses must not include a body.
Here's the reliable, spec-compliant fix that works with your setup:
Use Flask's make_response to create a body-less 204 response
Instead of returning a tuple with a body (even an empty one), explicitly create a response object, set its status code to 204, and leave its content empty. This tells the Tornado server to send a valid 204 response without any body content.
First, import make_response from Flask (Connexion builds on Flask, so this is already available to you):
from flask import make_response
Then update your handle_sol003_notifications function to return this properly constructed response:
def handle_sol003_notifications(cls, vnf_id): notification = connexion.request.get_json() if connexion.request.method == 'GET': response = make_response() response.status_code = 204 return response if notification['notificationType'] == 'VnfLcmOperationOccurrenceNotification': payload = Sol003Yang.YangInput_Sol003_Sol003Rpc.from_dict( {"vnfinstanceid": notification['vnfInstanceId'], "notificationstatus": notification['notificationStatus'], "operationstate": notification['operationState'], "operation": notification['operation'], "vnflcmopoccid": notification['vnfLcmOpOccId'], }) task = get_api_provider().run_in_tasklet_loop(cls.DTS.query_rpc("I,/sol003:sol003-rpc", 0, payload)) # Return 204 for POST requests too response = make_response() response.status_code = 204 return response
Why this works:
make_response()creates an empty Flask Response object by default, with no attached content.- Setting
status_code = 204ensures the correct HTTP status is sent. - Since we don't add any body content to the response, Tornado will comply with the 204 spec and omit the body entirely, avoiding the earlier errors.
Shorthand alternative (if it works for your setup)
If you prefer a more concise line, you can try this:
return make_response('', 204)
In newer Flask/Connexion versions, Flask automatically strips the empty string body when the status code is 204. If you still hit errors with this, stick to the explicit response object approach above—it's more foolproof with Tornado.
内容的提问来源于stack exchange,提问作者Pradul MT

