Godot Version
v4.3 using GDscript
Question
Hello, I’m using an api called Brickognize, and i’ve been trying to upload an jpg image to it for weeks. In the Brickognize documentation they say for the /predict method a query_image as String($Binary) is required. I’m new to Godot and I can’t figure out why nothing is working. On my search for help I found this:
import requests
res = requests.post(
'https://api.brickognize.com/predict/',
headers={'accept': 'application/json'},
files={'query_image': ('test.jpg', open('test.jpg','rb'), 'image/jpeg')},
)
answer by the Brickognize creator on r/learnpython
I tried a lot of different ways but I just don’t get it. Would be amazing if someone could tell me what to do.
What have you tried that doesn’t work?
I’ve tried for example converting the jpg to a binary by various tries I’ve all deleted. I have my trouble converting this jpg in a binary string and putting this in the right json.
m4tic
February 8, 2025, 3:43am
4
Adapted from
Proper syntax for multipart/form-data using Godot4 - #4 by mrcdk
mrcdk deserves all the credit
I tested this locally and with your API and it seemed to work both times.
extends HTTPRequest
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
send_request()
func send_request() -> void:
var url = "https://api.brickognize.com/predict/"
var image = "res://image.png"
# Create some random bytes to generate our boundary value
var crypto = Crypto.new()
var random_bytes = crypto.generate_random_bytes(16)
var boundary = '--GODOT%s' % random_bytes.hex_encode()
# Setup the header Content-Type with our bundary
var headers = [
'Content-Type: multipart/form-data;boundary=%s' % boundary
]
# Load the image and get the png buffer
var img = load(image).get_image() as Image
var buffer = img.save_png_to_buffer()
# Create our body
var body = PackedByteArray()
append_line(body, "--{{boundary}}".format({"boundary": boundary}, "{{_}}"))
append_line(body, 'Content-Disposition: form-data; name="query_image"; filename="my_image.png"')
append_line(body, 'Content-Type: image/png')
append_line(body, 'Content-Transfer-Encoding: binary')
append_line(body, '')
append_bytes(body, buffer)
append_line(body, "--{{boundary}}--".format({"boundary": boundary}, "{{_}}"))
request_raw(url, headers, HTTPClient.METHOD_POST, body)
func append_line(buffer:PackedByteArray, line:String) -> void:
buffer.append_array(line.to_utf8_buffer())
buffer.append_array('\r\n'.to_utf8_buffer())
func append_bytes(buffer:PackedByteArray, bytes:PackedByteArray) -> void:
buffer.append_array(bytes)
buffer.append_array('\r\n'.to_utf8_buffer())
func _on_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
print("Complete: ", result)
print("Code: ", response_code)
print("Body: ", body.get_string_from_ascii())
You’re my hero! I feel lowkey bad for don’t finding this articel my own, but thank you very much for your help.
I just added this line of code in _ready function:
request_completed.connect(_on_request_completed)
Now everything is working perfect.
system
Closed
March 11, 2025, 4:34pm
6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.