I have a basic python server like this:
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
class ServerHandler(BaseHTTPRequestHandler):
def do_POST(self):
print("got post")
content_len = int(self.headers.get('Content-Length'))
print(content_len)
post_body = json.loads(self.rfile.read(content_len))
self.send_response(200)
self.send_header('Content-type',"application/json")
self.end_headers()
self.wfile.write(b'{"message":"General Kenobi"}')
server= HTTPServer(('',6000),ServerHandler)
server.serve_forever()
print("server started...")
and I'm sending my post request from godot (gdscript) like this:
tool
extends HTTPRequest
export (bool) var send_req_btn = false setget send_req
func send_req(val):
if(val):
# Create an HTTP request node and connect its completion signal.
var body = to_json({"message": "Hello there"})
var error=self.request("http://localhost:6000", ['Content-Length:'+str(body.length())], true, HTTPClient.METHOD_POST, body)
if error != OK:
push_error("An error occurred in the HTTP request.")
func _on_HTTPRequest_request_completed(result, response_code, headers, body):
print("result: ",result)
print("response_code: ",response_code)
print("headers:",headers)
print("body:",body)
but it prints this as response in godot
result: 0 response_code: 200 headers:[Server: BaseHTTP/0.6 Python/3.10.4, Date: Thu, 22 Sep 2022 05:47:57 GMT, Content-type: application/json] body:[]
why can't I get the body of the response sent back by python?
source https://stackoverflow.com/questions/73810033/empty-body-as-response-from-server
Comments
Post a Comment