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
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.
-
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?– syzygyMar 10, 2017 at 1:20
-
I think you can make changes with
ipfs configat 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.– syzygyMar 10, 2017 at 1:35
-
1The 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
-
1Also 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
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).