Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs new file API example does not work #60

Open
zedomel opened this issue Jun 5, 2024 · 0 comments
Open

Nodejs new file API example does not work #60

zedomel opened this issue Jun 5, 2024 · 0 comments

Comments

@zedomel
Copy link

zedomel commented Jun 5, 2024

The nodejs example at: https://github.com/zenodo/developers.zenodo.org/blob/master/source/includes/overview/_quickstart.md does not work for me.

I manage it to work using the follow code:

const fs = require('fs');
const axios = require('axios');

const filePath = '<FILE_PATH>'; // Replace with file path
const bucketURL = '<BUCKET_URL>'; // Replace with bucket url
const fileName = '<FILE_NAME>'; // Replace with file name
const token = 'TOKEN'; // Replace with token value

// Read file as a stream
const stream = fs.createReadStream(filePath);

// Create request
let url = `${bucketURL}/${fileName}`;

let headers = {
    'Content-type': 'application/octet-stream',
    'Content-Length': fs.statSync(filePath).size //THIS IS IMPORTANT,
    'Authorization': `Bearer ${token}`
}

const requestConfig = {    
    headers: headers,
}

axios.put(url, stream, requestConfig).then(response => {
  console.log(response.data);
}).catch(error => {
  console.log(error.response.data);
});

Using FormData changes the Content-type to multipart-form and the bucket endpoint does not accept it. It requires application/octet-stream' (binary). Additionally, the Content-Length` must be set.

Hope this can help others.

Thanks.

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

No branches or pull requests

1 participant