Topic was automatically imported from the old Question2Answer platform.
Asked By
wojtasss
it is possible to upload file in godot using HTTPRequest node or I need use HTTPClient and do everything from scratch building raw http multipart request?
For small files, you could send a POST request using HTTPRequest and pass the binary data converted to Base64 in the request data using Marshalls.raw_to_base64(array: PoolByteArray). (This will increase the payload size by at least 33%.)
Calinou | 2020-08-07 13:51
Thanks @Callinou for for answer, but I have figured out how to make it with multipart form-data
I have found solution myself, here you are, this code is sending directly icon.png from res:// code doesn’t processing resposne, just send request as multipart form-data by HTTPClient.raw_request, as server backend I am using Rails with JSON format endpoint /images
func _ready():
var file = File.new()
file.open('res://icon.png', File.READ)
var file_content = file.get_buffer(file.get_len())
var body = PoolByteArray()
body.append_array("\r\n--WebKitFormBoundaryePkpFF7tjBAqx29L\r\n".to_utf8())
body.append_array("Content-Disposition: form-data; name=\"image\"; filename=\"icon.png\"\r\n".to_utf8())
body.append_array("Content-Type: image/png\r\n\r\n".to_utf8())
body.append_array(file_content)
body.append_array("\r\n--WebKitFormBoundaryePkpFF7tjBAqx29L--\r\n".to_utf8())
var headers = [
"Content-Type: multipart/form-data;boundary=\"WebKitFormBoundaryePkpFF7tjBAqx29L\""
]
var http = HTTPClient.new()
http.connect_to_host("http://localhost", 3000, false)
while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
http.poll()
OS.delay_msec(500)
assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect
var err = http.request_raw(HTTPClient.METHOD_POST, "/images" , headers, body)
assert(err == OK) # Make sure all is OK.
while http.get_status() == HTTPClient.STATUS_REQUESTING:
# Keep polling for as long as the request is being processed.
http.poll()
if not OS.has_feature("web"):
OS.delay_msec(500)
else:
yield(Engine.get_main_loop(), "idle_frame")