box / boxcli

A command line interface for interacting with the Box API.
https://developer.box.com
Apache License 2.0
222 stars 59 forks source link

how to request "zip_downloads" API with box cli? #455

Closed tttt-ffff closed 1 year ago

tttt-ffff commented 1 year ago

Description of the Issue

I tried below on Powershell v5.1 but not succeeded.

box request "https://api.box.com/2.0/zip_downloads" --method=POST --body='{"download_file_name":"test-*****(timestamp)*****","items":[{"type":"folder","id":"*****(folder id)*****"}]}' --verbose

When I use Inveke-RestMethod, I succeeded.

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization','Bearer *****(ACCESS TOKEN)*****')
$url = "https://api.box.com/2.0/zip_downloads" -replace "\uFEFF",""
$body = @"
{
    "download_file_name": "test-${timestamp}",
    "items": [
        {
            "type": "folder",
            "id"  : "*****(folder id)*****"
        }
    ]
}
"@
$response_zip = Invoke-RestMethod -Uri $url -Headers $headers -Body $body -Method "POST" -UseBasicParsing

Steps to Reproduce

as noted aboce

Expected Behavior

Error Message, Including Stack Trace

box:@box/cli:hooks:init done +0ms box init version: @oclif/command@1.5.5 argv: [ 'request', 'https://api.box.com/2.0/zip_downloads', '--method=POST', '--body={download_file_name:test-(timestamp),items:[{type:folder,id:(folder id)}]}', '--verbose' ] +0ms box-cli:init Initializing Box CLI +0ms box-cli:init Loaded settings { box-cli:init boxReportsFolderPath: 'C:\Users\Administrator\Documents\Box-Reports', box-cli:init boxReportsFileFormat: 'txt', box-cli:init boxDownloadsFolderPath: 'C:\Users\Administrator\Downloads\Box-Downloads', box-cli:init outputJson: false, box-cli:init enableProxy: false, box-cli:init proxy: { url: null, username: null, password: null }, box-cli:init enableAnalyticsClient: false, box-cli:init analyticsClient: { name: null } box-cli:init } +0ms box-cli:init Using environment (name){ box-cli:init clientId: '(clientId)', box-cli:init clientSecret: '(clientSecret)', box-cli:init name: '(name)', box-cli:init cacheTokens: true, box-cli:init authMethod: 'oauth20' box-cli:init } +0ms box-cli:init SDK client settings { iterators: true, analyticsClient: [Object], request: [Object] } +0ms box-cli:execute Starting execution command: request argv: [ box-cli:execute 'https://api.box.com/2.0/zip_downloads', box-cli:execute '--method=POST', box-cli:execute '--body={download_file_name:test-(timestamp),items:[{type:folder,id:(folder id)}]}', box-cli:execute '--verbose' box-cli:execute ] +0ms box-cli:output Formatted output content for display +0ms box-cli:output Using txt output format +0ms box-cli:output { box-cli:output statusCode: 400, box-cli:output headers: { box-cli:output date: '(date)', box-cli:output 'content-type': 'application/json', box-cli:output 'transfer-encoding': 'chunked', box-cli:output 'x-envoy-upstream-service-time': '210', box-cli:output 'strict-transport-security': 'max-age=31536000', box-cli:output 'box-request-id': '(box-request-id)', box-cli:output via: '1.1 google', box-cli:output 'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"' box-cli:output }, box-cli:output body: { box-cli:output status: 400, box-cli:output type: 'error', box-cli:output message: 'Supported payload format is JSON', box-cli:output code: 'bad_request', box-cli:output context_info: null, box-cli:output help_url: 'https://developer.box.com/guides/api-calls/permissions-and-errors/common-errors/', box-cli:output request_id: '(box-request-id)' box-cli:output } box-cli:output } +0ms box-cli:output Processed human-readable output +16ms box-cli:output Writing output to terminal +0ms Status Code: 400 Headers: Date: '(date)' Content-type: application/json Transfer-encoding: chunked X-envoy-upstream-service-time: '210' Strict-transport-security: max-age=31536000 Box-request-id: (box-request-id) Via: 1.1 google Alt-svc: >- h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43" Body: Status: 400 Type: error Message: Supported payload format is JSON Code: bad_request Context Info: null Help URL: >- https://developer.box.com/guides/api-calls/permissions-and-errors/common-errors/ Request ID: (box-request-id) box-cli:output Finished writing output +0ms

Screenshots

Versions Used

Box CLI: @box/cli/3.6.0 win32-x64 node-v14.19.3 Operating System: Microsoft Windows [Version 10.0.17763.3532]

mgrytsai commented 1 year ago

Hi @tttt-ffff ,

I'm wondering why you use direct API call instead of CLI method(e.g. box files:zip NAME) to download files. Have you tried CLI method instead?

See here fore more details

tttt-ffff commented 1 year ago

Thank you @mgrytsai

Because of the requirements of our customer (Please let me skip the detail)

mwwoda commented 1 year ago

I was able to invoke zip_downloads endpoint with the following command

box request "https://api.box.com/2.0/zip_downloads" --body='{"download_file_name":"test-file.zip","items":[{"type":"folder","id":"<file_id>"}]}' --method=POST --verbose

It's very similar to yours. Please check if the input is a valid json, especially the timestamp part.

tttt-ffff commented 1 year ago

Thank you @mwwoda

Did you run with Powershell?

mwwoda commented 1 year ago

Indeed I have the same problem on Powershell 5 with this command. You need to escape the double quotes to pass the argument correctly, which can be a bit tricky in older versions of powershell

The following commands with escaped double quotes should do the trick.

box request "https://api.box.com/2.0/zip_downloads" --body='{"""download_file_name""":"""test-file.zip""", """items""":[{"""type""":"""folder""","""id""":"""1"""}]}' --method=POST --verbose.

You can also try using the "here" string syntax - @" text"@.

In powershell 7, the original command I posted earlier works without any escape syntax.

tttt-ffff commented 1 year ago

Thank you @mwwoda !! It works!! Thank you for your help!!