4

I am currently playing around with IPFS in a docker container. I am able to send HTTP requests to the API using curl. I created a small text file with some text. I would like to add the file with the content to the node and retrieve it afterwards.

So I did:

curl -F file=test.txt http://localhost:5001/api/v0/add 

and received the response

{"Name":"QmYregh1mU7otV4s37hXLKnJ2fk2e8yFbJmU9L9cM6yrQM",
 "Hash":"QmYregh1mU7otV4s37hXLKnJ2fk2e8yFbJmU9L9cM6yrQM",
"Size":"16"}

Now I would like to read the file with and save it on my local machine. I used

 curl http://localhost:5001/api/v0/cat?arg=QmYregh1mU7otV4s37hXLKnJ2fk2e8yFbJmU9L9cM6yrQM 

to retrieve the file. The response is

test.txt

My question is now: How do I get the content of the file?

3
  • you should try to cast that to a string, basically toString() on the received payload Nov 27, 2019 at 10:55
  • Cast? I used the commands in a terminal. How could a cast help here?
    – Donut
    Nov 27, 2019 at 12:13
  • I suggest you try something like postman and see if you can change the preview of the response Nov 27, 2019 at 15:04

3 Answers 3

3

Try to use cat:

curl -X POST "https://ipfs.elastoscarrier.org/api/v0/cat?arg=QmYregh1mU7otV4s37hXLKnJ2fk2e8yFbJmU9L9cM6yrQM"

This will return the content of the added file.

0

To download IPFS objects using the HTTP API you need to use GET. As you can read in the docs.

/api/v0/get

Download IPFS objects.

Arguments

  • arg [string]: The path to the IPFS object(s) to be outputted. Required: yes.
  • output [string]: The path where the output should be stored. Required: no.
  • archive [bool]: Output a TAR archive. Required: no.
  • compress [bool]: Compress the output with GZIP compression. Required: no.
  • compression-level [int]: The level of compression (1-9). Required: no.

For your example, something like the following should work:

$ curl "http://localhost:5001/api/v0/get?arg=QmYregh1mU7otV4s37hXLKnJ2fk2e8yFbJmU9L9cM6yrQM&archive=false" >> myfile
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2048    0  2048    0     0   666k      0 --:--:-- --:--:-- --:--:--  666k
$ cat myfile
QmYregh1mU7otV4s37hXLKnJ2fk2e8yFbJmU9L9cM6yrQM0000644000000000000000000000001013567560642017027 0ustar0000000000000000test.txt

The content (test.txt) is shown at the end of the line. As you can see, I've used only one argument (archive=false) and doesn't work because I'm still getting a tar file. Please read about that here.

For your interest, it's possible to download objects using CLI commands:

get <ref>     Download IPFS objects

Example:

$ ipfs get QmYregh1mU7otV4s37hXLKnJ2fk2e8yFbJmU9L9cM6yrQM
Saving file(s) to QmYregh1mU7otV4s37hXLKnJ2fk2e8yFbJmU9L9cM6yrQM
 8 B / 8 B [========================================================================================================================================================] 100.00% 0s
$ cat QmYregh1mU7otV4s37hXLKnJ2fk2e8yFbJmU9L9cM6yrQM 
test.txt
0

Had the same problem and found the answer by looking at curl's manual. The issue has nothing to do with the IPFS API, it's the format of your curl command that is causing the problem.

instead of curl -F file=test.txt http://localhost:5001/api/v0/add, the correct command is curl -F [email protected] http://localhost:5001/api/v0/add with the added @ before the filepath.

Then to get a good output when you read the file, use api/v0/cat instead of api/v0/get.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.