2017-11-28 15 views
0

nodeJS를 통해 파일을 드롭 박스에 업로드하려고합니다. 이 CURL 요청이 작동합니다.데이터 - 요청 이진 노드

curl -X POST https://content.dropboxapi.com/2/files/upload \ 
--header "Authorization: Bearer myToken" \ 
--header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}" \ 
--header "Content-Type: application/octet-stream" \ 
--data-binary @fileName 

나는 어떻게 자바 스크립트 코드로 번역 해야할지 모르겠다. 는 여기에 지금까지 수행 한 작업은 다음과 같습니다

var request = require('request') 
    var headers = { 
     "Authorization": "Bearer " + dropboxToken, 
     "Dropbox-API-Arg": { 
          "path": "/"+fileName, //nome sul drive 
          "mode": "add", 
          "autorename": true, 
          "mute": false 
          }, 
     "Content-Type": "application/octet-stream" 
    } 
    var options = { 
     url: 'https://content.dropboxapi.com/2/files/upload', 
     method: 'POST', 
     headers: headers, 
    } 
    request(options, function (error, response, body) { 
     if (!error && response.statusCode == 200) { 
      console.log(body) 
     } 
    }); 

가 어떻게 업로드 할 파일을 선택하기 위해이 요청에서 data-binary 옵션을 포함합니까?

감사합니다. 당신이 readstream 한 다음 자바 스크립트의 보관 용 API와 통합하려는 경우는,

fs.createReadStream('/path/to/youfile').pipe(request.post(options).on('end', (done) => { console.log('success') })); 
+0

https://stackoverflow.com/a/4/495328/1757378 –

답변

0

당신을 위해 대부분의 작업을 할 것 같은 자바 스크립트 SDK v2의 공식 보관 용 API는 : 당신이 SDK를 사용하지 않으려면

https://github.com/dropbox/dropbox-sdk-js

그렇지 않으면, 당신은 요청 자신을 만들 수 있습니다. 이 경우 --data-binary 매개 변수는 curl에 대한 요청에 대한 데이터를 제공하기위한 curl 매개 변수입니다. 그런 다음 curl은 해당 데이터를 가져와 a Dropbox API v2 "content-upload" style endpoint (예 : /2/files/upload)의 파일 데이터를 제공하는 올바른 방법 인 요청 본문에 넣습니다.

그래서 요청 본문을 설정하는 방법에 대한 정보는 사용중인 HTTP 클라이언트의 설명서를 확인해야합니다. the request node module을 사용중인 것 같습니다. take a body option, where you can put the request data 인 것으로 보입니다.

0

첫째로 삼았 현재 헤더와 옵션을 요청할 파이프, 우리는을 사용하는 것이 좋습니다를 만들 수 있습니다

+0

다음과 같이 본문을 삽입하려고했습니다. var options = { url : 'https://content.dropboxapi.com/2/files/upload' , headers : headers, 본문 : '@'+ '/ myFile.txt' } ' 하지만 작동하지 않습니다. – Ford1892

+0

'@ '문법은'curl'에 로컬 파일을 지정하는 방법 일뿐입니다. 아마도 노드 요청 모듈에는 적용되지 않습니다. [해당 설명서] (https://github.com/request/request#requestoptions-callback)에서 데이터를 "Buffer", "String"또는 "ReadStream"으로 제공해야합니다. – Greg