0 votes

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: https://docs.ipfs.io/reference/http/api/#api-v0-multibase-encode

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.

Godot version 3.4.1.stable.official
in Engine by (70 points)

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);

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.