How do I add a file to http using ipfs? (without path)

I kinda just want to send to the api a file name (I actually don’t even care about it) I just want to give it “ABC” and let it return a CID to me.
I am fine for sepcifying what location in the DAG I want the file to be, I just want to start from this simple thing.
if there is a documentation I can read it myself too, didn’t seem to find a way to add a file without it already existing when I was reading HTTP API | IPFS Docs (ipns.localhost) though

To add files you would use ipfs add but I don’t understand what you mean by just a file name.

The DAG API is different then files, it allow structured data to be turned into blocks and linked to form DAGs.

I usually use json when using ipfs dag put

Docs → https://docs.ipfs.io/reference/cli/#ipfs-dag

I would like an example to add one file to ipfs, I am wondering how can you do it, without giving the path.


Like, what does it mean by path? how can I add a file?
is it a file system path on my machine like C:\Program Files(x86) or can it also be Google ?
the doc doesn’t say much

I am looking at HTTP API | IPFS Docs (ipns.localhost)

I hope someone can tell me if I can use http post to /api/v0/add
Then specify

name="<filename>"; filename="<file system path>"

In the header.
and then add the content of the file in the request body.

Is it possible to upload “ABC” to ipfs and add a file that contains “ABC”

The way you ask your question is actually very confusing, but I’m going to take a stab at it. Here is an example of what you can do with the cli:

> echo "hello worlds" | ipfs add
added QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx
> ipfs cat QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx
hello worlds

Once you have done that, you can use the following address to reach that file using an ipfs capable browser:

/ipfs/QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx
or
ipfs://QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx

Or, you could load it using a gateway, like this:

https://dweb.link/ipfs/QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx

Now, you could upload a folder structure instead, and then append a path into that structure when retrieving a file. What you cannot do, ever, is start with a path. Your address must start with a cid (or an ipns address pointing to a cid), optionally followed by a path if the cid points to a folder structure.

Does this help?

1 Like

I am trying to do it using http, do I put “Hello World” in the request body and that’s it?

You input was a good starting point for me too

also when I am using api/v0/pin/ls it’s not working

> echo "hello worlds" > hello.txt
> curl -X POST -F file=@hello.txt "http://localhost:5001/api/v0/add"
{"Name":"hello.txt","Hash":"QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx","Size":"21"}
> ipfs cat QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx
hello worlds
1 Like

What if I don’t want to use a file what if it’s an api in remote location what if I just want to give the string “hello world” to the api and it gives me back CID

class payload
{
    public string? content;
}
public class Program
{
    public static async Task Main(string[] args)
    {
        HttpClient httpClient = new HttpClient();
        var pl = new payload
        {
            content = "ABC"
        };
        var content = new StringContent(JsonSerializer.Serialize(pl, new JsonSerializerOptions
        {
            IncludeFields = true,
            DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
        }), Encoding.UTF8, "application/json");
        var res = await httpClient.PostAsync("http://localhost:5002/api/v0/add", content);
        Console.WriteLine(await res.Content.ReadAsStringAsync());
    }
}

I wrote a expample program in c# to post to my api

and I get

file argument 'path' is required

and I thought it’s ok to ignore path

js-ipfs/FILES.md at master · ipfs/js-ipfs (github.com)

I am looking at HTTP API | IPFS Docs (ipns.localhost), but it looks impossible to use. I am using c# trying to post data.
this doesn’t work :

        public static async Task Main(string[] args)
        {
            HttpClient httpClient = new HttpClient();
            var pl = new payload
            {
                data="wadjaoidwjawdi"
            };
            var content = new StringContent(JsonSerializer.Serialize(pl, new JsonSerializerOptions
            {
                IncludeFields = true,
                DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
            }), Encoding.UTF8, "application/json");
            var res = await httpClient.PostAsync("http://localhost:60003/api/v0/files/write?create=true&arg=abc", content);
            Console.WriteLine(await res.Content.ReadAsStringAsync());

it keeps telling me the data is missing

I don’t want to tell ipfs where my file is, I just want to tell ipfs here’s “Hello Wrold”, put it in a file or an object whatever, then I get CID that I can use later