Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Unexpected token < in JSON at position 0 #905

Closed
Juanchobanano opened this issue Dec 7, 2018 · 7 comments
Closed

Unexpected token < in JSON at position 0 #905

Juanchobanano opened this issue Dec 7, 2018 · 7 comments
Labels

Comments

@Juanchobanano
Copy link

Hey IPFS team, I have a strange error when trying to add new files to IPFS.
Here's my code:

`

const ipfsClient = require('ipfs-http-client');
const ipfs = ipfsClient("ipfs.infura.io", "5001", {protocol: "http"});

let testFile = fs.readFileSync("./backend/images/imagen.jpg");
ipfs.add(testFile, (err, res) => { 
    if(err) { console.error(err); }
});

`

It always returns the error "SyntaxError: Unexpected token < in JSON at position 0".
The value of testFile variable is " <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff fe 00 3b 43 52 45 41 54 4f 52 3a 20 67 64 2d 6a 70 65 67 20 76 31 2e 30 20 28 75 73 69 ... > "

I hope you can help me :(

@alanshaw
Copy link
Contributor

alanshaw commented Dec 7, 2018

Is your server configured to use https? You're trying to connect via http.

@Juanchobanano
Copy link
Author

Thanks for your response @alanshaw :)
I was able to fixed it, I wasn't properly initializing my ipfsClient object, here's the new code:

`

const ipfsClient = require("ipfs-http-client");
const ipfs = new ipfsClient({ host: 'ipfs.infura.io', port: 5001, protocol: 'https' });

`
However, I have a new problem. When trying to list the elements of a directory uploaded to IPFS, I receive the same error. Here's my code:

`

const validCID = 'QmNXGopftrhhDcx9zJxBcdWdYm3bGSXQwT9quwB1sDmxX6'
ipfs.ls(validCID, function (err, files) {
    console.log(files, err);
});

`
files returns "undefined" and err returns "SyntaxError: Unexpected token < in JSON at position 0"
Thanks in advanced 👍

@daviddias daviddias transferred this issue from ipfs-inactive/interface-js-ipfs-core Dec 9, 2018
@daviddias
Copy link
Contributor

I tried opening that CID on the IPFS Gateways http://ipfs.io/ipfs/QmNXGopftrhhDcx9zJxBcdWdYm3bGSXQwT9quwB1sDmxX6 to diagnose and it never loads. Can you make a reproducible case for us to test this out?

@alanshaw
Copy link
Contributor

ping @Juanchobanano

@Juanchobanano
Copy link
Author

Juanchobanano commented Dec 13, 2018

Hey guys! Sorry for the late response.
This is the way you can reproduce my error:

`

 const ipfsClient = require("ipfs-http-client");
 const ipfs = ipfsClient({host: "ipfs.infura.io", port: "5001", protocol: "https"})
 const fs = require("fs");

 app.use("/ipfsBug", (req, res) => {

 // Returns buffer of both files.
 let hash;
 const file1 = fs.readFileSync("./backend/images/imagen.jpg");
 const file2 = fs.readFileSync("./backend/images/imagen2.jpg");

 let dataToLoad = [
    {
        path: "backend/images/imagen.jpg",
        content: file1
    },
    {
        path: "backend/images/imagen2.jpg", 
        content: file2
    }
 ];

 ipfs.add(dataToLoad, [{"wrapWithDirectory": true}], (err, data) => {
    if(err){
        console.log(err);
    }else{
        console.log(data);
    }

    folderHash = data[data.length - 1].hash

    console.log("trying to use ipfs.ls function...")

    // Ipfs.ls returns error mentioned above.
    ipfs.ls(folderHash, function(err, files){
        if(err){
            console.log(err);
        }else{
            console.log(files);
        }
    });

    // Ipfs.get returns the files stored correctly.
    ipfs.get(folderHash, function(err, files){
        if(err){
            console.log(err);
        }else{
            console.log(files); 
        }
    });

});

/*const validCID2 = 'QmVwrz43zSJ9JqYtVFZovzoTdVRY1rnF62bc1xzDC9NmxL'
ipfs.get(validCID2, function (err, files) {
    console.log("err :", err);
    console.log("files: ", files);
    files.forEach((file) => {
        console.log(file.path)
    });
});*/

res.status(200).json({
    message: "asd"
})

})
`
Hope this can help!

@alanshaw
Copy link
Contributor

ipfs.add(dataToLoad, [{"wrapWithDirectory": true}], (err, data) => {

Your options are wrapped in an array, this will probably result in the directory wrapper not being applied...

https://ipfs.io/api/v0/ls?arg=QmNXGopftrhhDcx9zJxBcdWdYm3bGSXQwT9quwB1sDmxX6 is working for me right now.

However your server is returning the response:

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

https://ipfs.infura.io:5001/api/v0/ls?arg=QmNXGopftrhhDcx9zJxBcdWdYm3bGSXQwT9quwB1sDmxX6

Hope that helps.

P.S. I've opened this issue to track getting better error messages for unexpected responses.

@ghost ghost removed the ready label Dec 13, 2018
@Nikhil-Bathula
Copy link

ipfs.get throwing Unexpected token < in JSON at position 0 error

This is my code

ipfs.get(validCID, async function (err, files) {
if (err) {
resolve([])
} else {
resolve(files)
}
})

I kept this functionality in a loop. Even though i am passsing validCID it is throwing this error.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants