2

I have this curl request working.

curl -v "https://developer.api.autodesk.com/oss/v2/buckets/:bucketName/objects/" 
-X "PUT" -H "Authorization: Bearer tokenGoesHere" 
-H "Content-Type: application/octet-stream" -T "forupload.rvt"

How can I write this in node with npm request module. I tried the following with 'request' and 'fs'. I get back "Token is not provided in the request".

    function uploadFile(bucketData){
        var uri = 'https://developer.api.autodesk.com/oss/v2/buckets/' + bucketData['bucketKey'] + '/objects/'
        var authorizationHeader = ' Bearer ' + bucketData['token'] // this works in other post/get requests
        var contentTypeHeader = 'application/octet-stream'
        var streamTarget = 'C:\\Users\\architech\\Desktop\\Forge Node\\Test.rvt';
        console.log(uri)
        console.log(authorizationHeader)
        console.log(contentTypeHeader)
        console.log(streamTarget)
// console output:
// https://developer.api.autodesk.com/oss/v2/buckets/bucketpqglrzt/objects/
// Bearer ....token....
// application/octet-stream
// C:\Users\architech\Desktop\Forge Node\Test.rvt

        request.put(
            {
                url: uri,
       //       preambleCRLF: true,
       //       postambleCRLF: true,    
                multipart: 
                [
                    {
                        'Authorization': authorizationHeader,
                        'Content-Type': contentTypeHeader,                   
                         body: fs.createReadStream(streamTarget)
                    },  
                ]         
            },

            function(error, response, body){ 
                if(!error){
                    console.log(body);
                }else{
                    console.log(error);
                }
            })
    }
silicon mode
  • 143
  • 8

1 Answers1

2

After trying several approaches, while I couldn't reproduce your specific problem, the trouble I had was with the binary attachment loading properly. Because createReadStream() runs asynchronously, it doesn't really seem to work the way the request docs say it should when added to the multipart or formData keys. Not sure why this is?

I got it working first using http://requestb.in - comparing the curl request to the same request constructed with Node. Here is the final, working version:

var request = require('request')
fs = require('fs')

var options = {
    uri: 'https://developer.api.autodesk.com/oss/v2/buckets/<yourBucket>/objects/<yourFile.ext>',
    headers: {
        'Content-Type': 'application/octet-stream',
        'Authorization': 'Bearer <token>'
    }
}

fs.createReadStream(__dirname + '/<fileName.ext>').pipe(request.put(options, function(err, response, body) {
    console.log(body)
    /*
        {
          "bucketKey" : "< yourBucket>",
          "objectId" : "urn:adsk.objects:os.object:brandontestbucket2/skyscpr1.3ds",
          "objectKey" : "<fileName.ext>",
          "sha1" : "...redacted...",
          "size" : 43791,
          "contentType" : "application/octet-stream",
          "location" : "https://developer.api.autodesk.com/oss/v2/buckets/< yourBucket>/objects/<fileName.ext>"
        }
    */
}))
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • I had an error at the end of my uri where I was not giving my uploaded file a name. Having made that correction, I have tried both your methods and I am getting back "multi-part content not allowed". Also how can I print my request to the console? Thanks for your response. – silicon mode Jan 25 '17 at 16:30
  • I'd suggest trying requestb.in: send your Node request and your Curl request to a bin, and compare the requests. That said, if the API is responding with "multi-part not allowed" then I'd suggest checking out their docs to figure out what the correct way to make the request is. – brandonscript Jan 25 '17 at 16:53
  • I decided to use child_process to run the curl request from command line and capture stdout. I will revisit this when necessary. Maybe api does not like how read stream serves up the file...I will accept this answer as it seems to be an accurate translation of the curl request posted however createReadStream may not play well with this autodesk api. Thanks. – silicon mode Jan 25 '17 at 16:59
  • I wouldn't recommend that solution, but if you have to, do what you gotta do. Challenge with child process is you lose the ability to manage the request and the response programatically within the Node ecosystem, and you rely on external Curl working as expected. – brandonscript Jan 25 '17 at 17:01
  • Also for what it's worth, I tried finding the `/oss` endpoint in their docs, but couldn't. If you can show me where it is in the docs, I can keep trying. – brandonscript Jan 25 '17 at 17:02
  • @ brandonscript Thanks for your help. Step #2 is the step I am trying to accomplish. https://developer.autodesk.com/en/docs/data/v2/tutorials/app-managed-bucket/ – silicon mode Jan 26 '17 at 14:18
  • Brilliant! Works great. Thank you. I have added Autodesk tags to this question. – silicon mode Jan 28 '17 at 17:26