ipfs-docs icon indicating copy to clipboard operation
ipfs-docs copied to clipboard

Clarify how to use the `x-directory` header in Curl requests.

Open autonome opened this issue 3 years ago • 5 comments

From a hackathon participant:

I am trying to add a file in IPFS inside a specific directory via the HTTP API, can someone point me to an example in cURL? curl -F "test.json=@/2021/03/18/weather.json" 127.0.0.1:5001/api/v0/add?wrap-with-directory=true the above does not seem to create a "weather.json" file inside a directory structure "/2021/03/18" but instead it creates a file called "/2021/03/18/weather.json"

One answer I got when asking around:

The api docs explain how the multipart body works. You cannot let curl build the multipart as normal, since you need to send a x-directory part apart from the uploaded file part i think, or at least set some part header manually with the path. I never got to figure out what curl foo magic is needed and whether this is possible.

If we don't know how to use it, nobody else will either 😅

autonome avatar Mar 19 '21 13:03 autonome

Updated answer for future use in examples:

curl -F '[email protected];encoder=base64;filename=a/b/c.gif' 'http://localhost:5001/api/v0/add?progress=true' {"Name":"a/b/c.gif","Bytes":33534} {"Name":"a/b/c.gif","Hash":"QmVCTMQrvAj1Czk4beNxD5LKP4sHYFwTwPDvc5UDTaGzPB","Size":"33548"} {"Name":"a/b","Hash":"Qmb8UKKhhqRUpgyhBK1HfdzNZKydRo5EwDj334BuUSLjPy","Size":"33601"} {"Name":"a","Hash":"Qma4t85aY6LoGYbasRidumoMZizkhRbVHkk8EV2u7DLGYg","Size":"33650"}

And docs link for the API:

https://docs.ipfs.io/reference/http/api/#request-body

Could be an easy fix to close this gap is to just put the full curl example in that part of the docs?

autonome avatar Mar 19 '21 13:03 autonome

So, there are two things at play here, x-directory and filename:

You can also put x-directory parts, they don't hurt, and are needed for example to create an empty dir I think, but otherwise things are inferred from the filename header.

autonome avatar Mar 19 '21 14:03 autonome

Just to make sure I'm understanding correctly:

  1. The current example just creates a file called /2021/03/18/weather.json:

    curl -F "test.json=@/2021/03/18/weather.json" 127.0.0.1:5001/api/v0/add?wrap-with-directory=true
    
  2. The actual command should be:

    curl -F "[email protected];encoder=base64;filename=a/b/c.json" http://localhost:5001/api/v0/add?progress=true
    

I'm confused about the x-directory bit. Is it required, or optional? I can't see it in any of the examples.

johnnymatthews avatar Mar 22 '21 14:03 johnnymatthews

Exactly. The documentation should say what each header is for, and why, with examples.

On Mon, Mar 22, 2021 at 10:22 AM Johnny @.***> wrote:

Just to make sure I'm understanding correctly:

The current example just creates a file called /2021/03/18/weather.json :

curl -F "test.json=@/2021/03/18/weather.json" 127.0.0.1:5001/api/v0/add?wrap-with-directory=true

The actual command should be:

curl -F @.***;encoder=base64;filename=a/b/c.json" http://localhost:5001/api/v0/add?progress=true

I'm confused about the x-directory bit. Is it required, or optional? I can't see it in any of the examples.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ipfs/ipfs-docs/issues/704#issuecomment-804101402, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAMHN3J5JEUJGDBJ7DGLX3TE5HA7ANCNFSM4ZO2AAJA .

autonome avatar Mar 22 '21 15:03 autonome

would really appreciate help on this. My use case is uploading the 10k metadata files for an NFT collection. I cannot construct a valid request in Python since that requires opening 10k individual files. Can't find any information on the web on how to do this.

nazariyv avatar Sep 25 '21 16:09 nazariyv