Skip to content

Instantly share code, notes, and snippets.

@DrI-T
Last active March 28, 2021 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrI-T/902dd9d39bc6347e5f49b0f4e280fc56 to your computer and use it in GitHub Desktop.
Save DrI-T/902dd9d39bc6347e5f49b0f4e280fc56 to your computer and use it in GitHub Desktop.
CORS and local IPFS API gateway ...
<!DOCTYPE html><meta charset="utf8"><link rel=icon href=favicon.ico>
<title>Setting CORS for your IPFS API gateway</title>
<h2>Setting CORS for your IPFS API gateway</h2>
<p>
Prior to click the fetch button please
make sure your "Allow-Origin" list is set properly by running the following command in your shell
(assumed you have ipfs &amp; screen on your machine) :
<pre style="border: 1px solid grey; overflow: scroll; padding: 1em;"><code>
export IPFS_PATH=$HOME/.ipfs
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://yoursite.localhost:8080",
"http://127.0.0.1:8080","http://localhost:3000",
"http://127.0.0.1:48084", "https://gateway.ipfs.io", "https://webui.ipfs.io"]'
# then "restart" your daemon :
ipfs shutdown
screen -dmS IPFS ipfs daemon
screen -list
ipfs config API.HTTPHeaders.Access-Control-Allow-Origin
</code></pre>
<div>
API endpoint: <input name=url size=94>
<input type=submit value=fetch onclick="post(event)">
</div>
<h4>result</h4>
<div id=resp style="border: 1px solid blue; width: 80vw; overflow-wrap: anywhere;"></div>
<script>
document.getElementsByTagName('code')[0].innerHTML =
document.getElementsByTagName('code')[0].innerHTML.replace('http://yoursite.localhost:8080',location.origin);
const api_url = 'http://127.0.0.1:5001/api/v0/';
var url = api_url + 'config?arg=API.HTTPHeaders.Access-Control-Allow-Origin';
document.getElementsByName('url')[0].value = url;
function post(ev) {
let url = document.getElementsByName('url')[0].value;
return fetch(url, { method: 'post', mode: 'cors' }).
then( resp => {
if (resp.ok) { return resp.json(); }
else { return { "Error": resp.Status }; }
}).
then( json => {
console.log('json:',json);
document.getElementById('resp').innerText = 'resp: ' + JSON.stringify(json);
}).
catch(console.error)
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment