IPNS resolve using HTTP Request won't work

I am testing IPNS resolve using http POST request, testing codes as following:

var http_Req = new XMLHttpRequest();
http_Req.addEventListener(“load”, reqListener);
var url = “http://127.0.0.1:5001/api/v0/resolve?arg=/ipns/QmSX49hMU7ruJey8hGUuA4FCebYXRESayCeD41mHdhARrN”;
http_Req.open(“POST”, url, true);
http_Req.send();

But got response error with 403 Forbidden.

Using CLI,
curl -X POST http://127.0.0.1:5001/api/v0/resolve?arg=/ipns/QmSX49hMU7ruJey8hGUuA4FCebYXRESayCeD41mHdhARrN
{“Path”:"/ipfs/QmYpKRffRktcD8tc716FKK8MEUoTJoLTa6cqHUBzHHaNGm"}

However, curl command just shows as what I expect.

Any idea?

CORS configuration of go-ipfs. You need to allow the origin set by the browser. Check there actual request made by the browser, as it goes with original headers that your curl request does not set.

go-ipfs has some provisions to detect if requests come from a browser and only let’s them through when CORS origin is set accordingly.

2 Likes

Thanks for the reply. The CORS issue was OK after go-ipfs config setting.

Now, I intend to use fetch to send a HTTP request from the browser to go-ipfs at my desktop in order to resolve an IPNS peer id.

In browser end, I used the following test codes, but console log showed “undefined” CID.

I am wondering if ipns resolve can be done this way (fetch http request)?

Thank you for your help.

example codes:
go-ipfs version: 0.5.1

var url = ‘http://127.0.0.1:5001/api/v0/resolve?arg=/ipns/QmFixedPeerID’;

function fetchCID(url){
const response = fetch(url, {
headers: {
‘content-type’: ‘application/x-www-form-urlencoded’
},
cache: ‘no-cache’,
credentials: ‘omit’,
method: ‘POST’,
mode: ‘cors’
})
.then(
(response) => {
if (response.ok){
// Examine the CID in the response
var json = response.json();
var CID = json.Path;
console.log(CID:${CID});
return (CID);
}
})
.catch(function(err) {
console.log(‘Fetch Error :’, err);
})
}