2

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?

  • you should try to cast that to a string, basically toString() on the received payload – Kaki Master Of Time Nov 27 '19 at 10:55
  • Cast? I used the commands in a terminal. How could a cast help here? – Donut Nov 27 '19 at 12:13
  • I suggest you try something like postman and see if you can change the preview of the response – Kaki Master Of Time Nov 27 '19 at 15:04
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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