How to POST via curl

I am looking for the curl equivalent of ipfs add filename.ext -w for curl.
I want to curl -v -X POST --binary-data @filename http://localhost:8080/, but it doesnt retain the filename.

Anyone know how to do it?

Handy general tip when you want to figure out what the go-ipfs cli command would look like for cURL, run tcpdump and grep for POST:

sudo tcpdump -A port 5001 -i any | grep POST

This will print out all the POST request URLs that you’ll make to port 5001 on local interfaces. We can then see:

tcpdump: data link type PKTAP
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type PKTAP (Apple DLT_PKTAP), capture size 262144 bytes
,.>.,.>.POST /api/v0/add?encoding=json&progress=true&stream-channels=true&w=true HTTP/1.1
,.>.,.>.POST /api/v0/add?encoding=json&progress=true&stream-channels=true&w=true HTTP/1.1

So what you have to add in the end, is ?w=true to your request URL.

1 Like

I realize now that you’re dealing with the writable gateway rather than the API, so my answer is not applicable. But I’ll leave it there, in case it’s useful for someone else in the future.

Regarding wrapping a file when adding via the gateway, I’m unsure of if it’s possible…

I thought I saw that was an option at one point.

Good news! Your thought was correct and it is possible to add files to a empty directory.

First, QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn is the hash of a empty directory (see the structure with ipfs object get QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn), and with that, we can create new DAG that includes our newly created file.

A example:

curl -v -X PUT --data 'hello world' http://localhost:8081/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn/hello

This would use the existing empty directory, create a file called hello and add the content hello world.

The resulting hash can be found in the Ipfs-Hash header and the full path can be found in the Location header.

1 Like

Yes, that’s exactly it.

So, as it turns out, you can use any DAG, the difference is between a POST and a PUT.

curl -F “file=@ipfs” “http://localhost:5001/api/v0/add?recursive=true” this command not work and ipfs is a folder, anyone know why?