Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | stubbsy345 | |
Old Version | Published before Godot 3 was released. |
I am using the following code to attempt to connect to a local server I have set up using SimpleHTTPServer. However, every time i get the error message saying the the assertion has failed. I have tried numerous different url’s in the connect function but nothing is working. I am unfamiliar with connecting to servers so any help would be greatly appreciated.
func talkToServer(url, mode, data):
# Connect to host/port
HTTP = HTTPClient.new()
RESPONSE = HTTP.connect("http://localhost:8000/", 8000)
# Wait until resolved and connected
while HTTP.get_status() == HTTPClient.STATUS_CONNECTING or HTTP.get_status() == HTTPClient.STATUS_RESOLVING:
HTTP.poll()
OS.delay_msec(300)
# Error catch: Could not connect
assert(HTTP.get_status() == HTTPClient.STATUS_CONNECTED)
# Check for a GET or POST command
if data == "":
HEADERS =["User-Agent: Pirulo/1.0 (Godot)", "Accept: */*"]
RESPONSE = HTTP.request(HTTPClient.METHOD_GET, url, HEADERS)
else:
QUERY = HTTP.query_string_from_dict(data)
HEADERS = ["User-Agent: Pirulo/1.0 (Godot)", "Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(QUERY.length())]
RESPONSE = HTTP.request(HTTPClient.METHOD_POST, url, HEADERS, QUERY)
# Make sure all is OK
assert(RESPONSE == OK)
# Keep polling until the request is going on
while (HTTP.get_status() == HTTPClient.STATUS_REQUESTING):
HTTP.poll()
OS.delay_msec(300)
# Make sure request finished
assert(HTTP.get_status() == HTTPClient.STATUS_BODY or HTTP.get_status() == HTTPClient.STATUS_CONNECTED)
# Set up some variables
var RB = RawArray()
var CHUNK = 0
var RESULT = 0
# Raw data array
if HTTP.has_response():
# Get response headers
var headers = HTTP.get_response_headers_as_dictionary()
while HTTP.get_status() == HTTPClient.STATUS_BODY:
HTTP.poll()
CHUNK = HTTP.read_response_body_chunk()
if(CHUNK.size() == 0):
OS.delay_usec(100)
else:
RB = RB + CHUNK
HTTP.close()
RESULT = RB.get_string_from_ascii()
I am getting the following error message in my terminal:
127.0.0.1 - - [03/Oct/2017 17:01:57] code 501, message Unsupported method ('POST')
127.0.0.1 - - [03/Oct/2017 17:01:57] "POST blankJSON.json HTTP/1.1" 501 -