rest - Upload a file to Box.com using Powershell -


i working on series of powershell scripts company use transfer data , box.com. 1 thing can't figure out uploading files. box api requires multipart post operation uploads, , i've seen few answers here on indicating should able in powershell (such this one). can't seem working.

here's code have right now:

function post-file {     param(             [parameter(mandatory=$true,position=1)]             [string]$sourcepath,             [parameter(mandatory=$false,position=2)]                         [string]$folderid = ############     )      #variables building uris     $baseurl = "https://upload.box.com/api/2.0/files/content"      #set authorization api requests     $headers = @{}     $accesstoken = refresh-tokens #a reference function works     $headers.add("authorization", "bearer $accesstoken")      #set post content     $body = @{}     $body.add("filename",  [io.file]::readallbytes($sourcepath))     $body.add("parent_id", $folderid)      #upload file     invoke-restmethod -uri $baseurl -method post -headers $headers -contenttype "multipart/form-data" -body $body } 

here's response back:

{  "type":"error",  "status":400,  "code":"invalid_request_parameters",  "help_url":"http://developers.box.com/docs/#errors",  "message":"invalid input parameters in request",  "request_id":"1764475572534bcddfe04b7" } 

i've tried couple of other permutations aren't working. i've tried using -infile switch in invoke-restmethod instead of -body. i've tried using get-content -raw in place of [io.file]::readallbytes. both of return more generic error: the server encountered internal error or misconfiguration , unable complete request..

i'm pretty sure has filename parameter not being correct, i'm not sure how fix it. according box api, here's should in curl. can me translate powershell?

https://upload.box.com/api/2.0/files/content \ -h "authorization: bearer access_token" \ -f filename=@file_name \ -f parent_id=parent_folder_id 

there couple things make little tricky in powershell:

  1. the filename parameter specifies name of file, not contents.
  2. a request body required in order specify file name , destination.
  3. powershell treats -infile , -body arguments mutually exclusive.
  4. powershell not appear natively support multipart/form-data posts, per question referenced.

since request body required (1,2) -- , -infile can't used (3) -- think might need roll own multipart/form-data body (4) contains necessary metadata , file content. this blog post describes method doing so. content there short string (a tweet) principle same.

below fiddler trace of upload performed using box windows sdk. shows how request should goes across wire. $boundary arbitrary, unique string -- guid works great.

post https://upload.box.com/api/2.0/files/content http/1.1 authorization: bearer $token content-type: multipart/form-data; boundary="$boundary" host: upload.box.com content-length: 2118 accept-encoding: gzip, deflate  --$boundary  content-disposition: form-data; name="file"; filename="$filename"  <the content of $filename>      --$boundary  content-type: text/plain; charset=utf-8 content-disposition: form-data; name="metadata"  {"parent":{"id":"$parent_folder_id"},"name":"$filename"}  --$boundary-- 

here response received:

http/1.1 201 created date: mon, 14 apr 2014 12:52:33 gmt server: apache cache-control: no-cache, no-store content-length: 364 connection: close content-type: application/json  {"total_count":1,"entries":[{"type":"file","id":"$id","name":"$filename", ... }]} 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -