简体   繁体   English

如何使用“请求”模块在节点中写入curl“ PUT”请求

[英]How to write curl 'PUT' request in node with 'request' module

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. 我该如何在带有npm request模块的节点中编写此代码。 I tried the following with 'request' and 'fs'. 我用“ request”和“ 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);
                }
            })
    }

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. 由于createReadStream()是异步运行的,因此当添加到multipartformData键时,它似乎并没有按照请求文档所说的那样工作。 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. 我首先使用http://requestb.in使它工作-将curl请求与用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>"
        }
    */
}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM