Skip to content

Instantly share code, notes, and snippets.

@nisovin
Last active April 10, 2024 20:31
Show Gist options
  • Save nisovin/62f7b01d669e20557095b2f251405e3f to your computer and use it in GitHub Desktop.
Save nisovin/62f7b01d669e20557095b2f251405e3f to your computer and use it in GitHub Desktop.
extends HTTPRequest
class_name HTTPFormPost
func example():
post_files("https://example.com", [
FileDef.new().from_file("user://test.txt", "file")
], {
"other_field": "test"
})
func post(url: String, fields: Dictionary, custom_headers: Array = [], verify_ssl: bool = true):
var field_pairs = []
for key in fields:
var val = fields[key]
if val is Array:
for array_val in val:
field_pairs.append(key + "[]=" + str(array_val).http_escape())
else:
field_pairs.append(key + "=" + str(val).http_escape())
custom_headers.append("content-type: x-www-form-urlencoded")
request(url, custom_headers, verify_ssl, HTTPClient.METHOD_POST, "&".join(field_pairs))
func post_files(url: String, files: Array, post_fields: Dictionary = {}, custom_headers: Array = [], verify_ssl: bool = true):
var boundary := "---------------------------" + str(randi()) + str(randi())
custom_headers.append("content-type: multipart/form-data; boundary=" + boundary)
var body = PoolByteArray()
var boundary_bytes := ("--" + boundary + "\r\n").to_utf8()
for key in post_fields:
body.append_array(boundary_bytes)
body.append_array(("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n" + post_fields[key] + "\r\n").to_utf8())
for file in files:
body.append_array(boundary_bytes)
body.append_array(("Content-Disposition: form-data; name=\"" + file.field_name + "\"; filename=\"" + file.file_name + "\"\r\nContent-Type: " + file.content_type + "\r\n\r\n").to_utf8())
body.append_array(file.content_bytes)
body.append_array("\r\n".to_utf8())
body.append_array((boundary + "--").to_utf8())
request_raw(url, custom_headers, verify_ssl, HTTPClient.METHOD_POST, body)
class FileDef:
var field_name: String = ""
var file_name: String = ""
var content_type: String = ""
var content_bytes: PoolByteArray
func from_file(path: String, field_name: String, file_name: String = "", content_type: String = "application/octet-stream"):
self.field_name = field_name
self.file_name = path.get_basename()
self.content_type = content_type
var file = File.new()
var err = file.open(path, File.READ)
if err == OK:
content_bytes = file.get_buffer(file.get_len())
file.close()
return self
func from_text_file(path: String, field_name: String, file_name: String = "", content_type: String = "text/plain"):
from_file(path, field_name, file_name, content_type)
return self
func from_bytes(content: PoolByteArray, field_name: String, file_name: String, content_type: String = "application/octet-stream"):
self.field_name = field_name
self.file_name = file_name
self.content_type = content_type
self.content_bytes = content
return self
func from_text(content: String, field_name: String, file_name: String, content_type: String = "text/plain"):
from_bytes(content.to_utf8(), field_name, file_name, content_type)
return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment