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

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

the path refers to a file that is accessible to your filesystem. like a mounted drive.

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

Thanks so much for the help.
I figured it out myself that when posting the file I can’t specify the parameters in the datacontent of the post request, at least it’s for me.

I figured out that in c# the request should be MultipartFormDataContent.
And you post it using

var dataContent = new MultipartFormDataContent();
            var content = new StringContent("12345");
var res = await client.PostAsync(
                          $"{host}/api/v0/files/write?create=true&arg=/{filename that you want to create in ipfs MFS}",
                          dataContent);
            dataContent.Add(content);

this shows it’s possible to put “hello world” to ipfs without creating “hello world.txt”