Search code examples
node.jshttpexpressrequestautodesk

Send curl with Expect --upload-file header -> Node.js


I'm working on uploading file to Autodesk View and Data API and i cant figure out how to send curl by http, or request lib. To be specific i don't know how to set expect header.

Here is the curl i try to send

curl --header "Authorization: Bearer nqjy2YlqYVi9VqBLjKinixIkhDRA" --header "Content-Length: 199233" \
-H "Content-Type:application/octet-stream" --header "Expect:" --upload-file "./Torus.obj" 
-X PUT https://developer.api.autodesk.com/oss/v1/buckets/mybucket/objects/Torus.obj -k

Everytime i get 417 http response. Can someone help me to develop function in Node.js?

Many Thanks.

---UPDATE---- Fixed:

var xhr = new XMLHttpRequest();

xhr.open('PUT',
    'https://developer.api.autodesk.com/oss/v1/buckets/' + GLOBAL.AutodeskBucket + '/objects/' + file.name,
    true);

xhr.setRequestHeader(
  'Authorization',
  'Bearer ' + GLOBAL.AutodeskToken.access_token);

xhr.setRequestHeader(
  'Content-Type',
  'application/stream');

xhr.onload = function () {
    var response = JSON.parse(xhr.responseText);
    response.file = file;
    callback(response);
};


var reader = new FileReader();

// `onload` as listener
reader.addEventListener('load', function (ev) {
  xhr.send(ev.target.result);
});

var file = new File(path.dirname(require.main.filename) + '/files/' + file.name);

reader.readAsBinaryString(file);

Solution

  • Just in case, you may also check the documentation at https://developer.autodesk.com/api/view-and-data-api/#oss-bucket-and-object-api-v1-0