How to make a call to IPFS HTTP API that requires data in the request body?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tx350z

I am attempting to make calls to go-ipfs HTTP API using HttpRequest. All calls to the API must use METHOD_POST. Calls to API commands that do not require a body are not a problem, they are working fine. However, I’m struggling to get the headers and body correct for commands that require a body. For example, the IPFS multibase/encode command requires a body containing the data to be encoded.

This is a link to the documentation for multibase/encode: HTTP APIs for IPFS | IPFS Docs

I’ve been looking for an working example for several days. The best I’ve found is examples for js-ipfs which do not help since the IPFS command functions can be called directly from javascript.

Assume I want to use HttpRequest.request to POST a file named “test_data.txt” to IPFS HTTP API multibase/encode. What needs to be in the headers array and what is the structure of the body?

Many thanks in advance.

I managed to get a successful response. However, I’m not sure it’s “clean” enough to work in all cases. For those who are interested, here is the code to make the request to IPFS HTTP API multibase/encode:

func _on_send_pressed()->void:
	var df:File = File.new();
	df.open("res://ipfs_http_api_test.txt",File.READ);
	var ftxt = df.get_as_text();
	df.close();
	var headers:Array = [
		"Content-Type: multipart/form-data; boundary=\"BodyPart35674573458\""
	];
	var body;
	body = "\r\n--BodyPart35674573458\r\n"
	body += "Content-Disposition: form-data; name=\"file\"; filename=\"ipfs_http_api_test.txt\"\r\n";
	body += "Content-Type: application/json\r\n\r\n";
	body += JSON.print(ftxt);
	body += "\r\n--BodyPart35674573458\r\n";
	httpreq.request("http://127.0.0.1:5001/api/v0/multibase/encode",headers,false,HTTPClient.METHOD_POST,body);

tx350z | 2021-12-30 15:05