Unable to encode JSON in a Gitlab Yaml Pipeline

831 views Asked by At

I'm unable to submit Dropbox API request from YML Gitlab Pipeline using PowerShell (Windows runner). The API requires to pass a JSON as a header parameter and encoding that JSON into YML Gitlab Pipeline doesn't work:

`--header "Dropbox-API-Arg:{\"path\":\"/$BUILD_FILENAME\"}"

The full script step is below as it's now defined in YML:

- C:\Windows\System32\curl.exe -X POST https://content.dropboxapi.com/2/files/upload --header "Authorization:Bearer $DROPBOX_TOKEN" --header "Content-Type:application/octet-stream" --header "Dropbox-API-Arg:{\"path\":\"/$BUILD_FILENAME\"}" --data-binary @$BUILD_FULLFILENAME

The response error is below:

Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": could not decode input as JSONcurl: (3) Port number ended with '\'

I can execute the command above in Windows PowerShell on the same PC where the runner is without any issues:

curl -X POST https://content.dropboxapi.com/2/files/upload --header "Authorization: Bearer XXX" --header "Dropbox-API-Arg: {\"path\": \"/README.md\"}" --header "Content-Type: application/octet-stream" --data-binary @README.md

How can I encode the JSON properly so it can be executed and pass to the CURL as expected?

1

There are 1 answers

0
Patrick On BEST ANSWER

I think the following should work. It uses --% which prevents PowerShell from parsing the arguments of the curl command when invoking $fullCommand. This allows to keep the double quotes within the Dropbox-API-Arg header.

$fullCommand = 'C:\Windows\System32\curl.exe --%'
$fullCommand += ' -X POST https://content.dropboxapi.com/2/files/upload'
$fullCommand += ' --header "Authorization:Bearer ' + $DROPBOX_TOKEN + '"'
$fullCommand += ' --header "Content-Type:application/octet-stream"'
$fullCommand += ' --header "Dropbox-API-Arg:{\"path\":\"' + $BUILD_FILENAME + '\"}"'
$fullCommand += ' --data-binary @' + $BUILD_FULLFILENAME
Invoke-Expression $fullCommand

Live Demo

.gitlab-ci.yml

stages:
  - run_on_windows_test

run_on_windows_test_1:
  stage: run_on_windows_test
  tags:  
  - shared-windows
  - windows
  - windows-1809

  variables:
    DROPBOX_TOKEN: "THE_TOKEN"
    BUILD_FILENAME: "test.txt"
    BUILD_FULLFILENAME: C:\Windows\TEMP\test.txt
  
  script:
    - fsutil file createnew C:\Windows\TEMP\test.txt 128
    - $fullCommand = 'C:\Windows\System32\curl.exe --% '
    - $fullCommand += '-X POST https://requestinspector.com/inspect/01ems2t2f0r4dkfyzk1d820e1t'
    - $fullCommand += ' --header "Authorization:Bearer ' + $DROPBOX_TOKEN + '"'
    - $fullCommand += ' --header "Content-Type:application/octet-stream"'
    - $fullCommand += ' --header "Dropbox-API-Arg:{\"path\":\"' + $BUILD_FILENAME + '\"}"'
    - $fullCommand += ' --data-binary @' + $BUILD_FULLFILENAME
    - Invoke-Expression $fullCommand
    - $fullCommand = 'C:\Windows\System32\curl.exe --% '
    - $fullCommand += '-X POST http://requestbin.net/r/1b135t41'
    - $fullCommand += ' --header "Authorization:Bearer ' + $DROPBOX_TOKEN + '"'
    - $fullCommand += ' --header "Content-Type:application/octet-stream"'
    - $fullCommand += ' --header "Dropbox-API-Arg:{\"path\":\"' + $BUILD_FILENAME + '\"}"'
    - $fullCommand += ' --data-binary @' + $BUILD_FULLFILENAME
    - Invoke-Expression $fullCommand