Skip to content

Nodejs new file API example does not work #60

@zedomel

Description

@zedomel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions