8

So I want to put a website on ipfs, but it has some javascript which calls out to a server that is not the ipfs gateway, so I get cross origin errors. Any idea how to do this?

2 Answers 2

14

You can set the Access-Control-Allow-Origin header and other headers using ipfs config:

ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["GET", "POST"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'

The values above are just examples; set the real values to what your client code actually needs.

https://docs.ipfs.io/reference/api/cli/#ipfs-daemon has the (minimal) existing docs on this.

10
  • Ooo this looks promising. So I do this before I do "ipfs add"? Or does this need to be done on the gateway before it is run?
    – syzygy
    Mar 10, 2017 at 1:20
  • I think you can make changes with ipfs config at any time and they take effect without need to restart. But don’t take my word for it… Mar 10, 2017 at 1:28
  • I guess what I'm missing is, does the daemon need this or is it the gateway? I realize they can be the same machine, but in the case they aren't.
    – syzygy
    Mar 10, 2017 at 1:35
  • 1
    The daemon needs it. Or I guess more precisely the API needs it, but it needs to be set by the daemon. The daemon then passes the headers to the API. I guess you can set the daemon to pass them to the gateway but I think you don’t need to Mar 10, 2017 at 1:37
  • 1
    Also I just did a fresh ipfs install (version 0.4.6) from scratch and I notice that the correct Access-Control-* headers already get set on the gateway by default, without me needing to configure anything… Mar 11, 2017 at 7:53
2

The syntax in the answer does not work for me on 2021-03-19. Fortunately, the browser-ipns-publish has an example. The syntax used there is

ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"*\"]"

Not sure if this is due to being run on Windows but it works-on-mine (TM).

0

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.