Home > Blockchain >  Curl in Shell Script Potentially Failing in Headers Section
Curl in Shell Script Potentially Failing in Headers Section

Time:12-03

I'm trying to run a script that would get a list of users in bitwarden and output it to a file called bitmembers.

Below is the script where XXXX is my API bearer token

#!/bin/bash

CURL="/usr/bin/curl"
BITHTTP="https://api.bitwarden.com/public/members"
CURLARGS="-X GET"

header='-H "accept: text/plain" -H "Authorization: Bearer XXXX"'

$CURL $CURLARGS $BITHTTP $header > /tmp/bitmembers

Below is the error I'm getting when script is ran:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: text
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: Bearer
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: XXXX

Below is the curl command and confirmed works:

curl -X GET "https://api.bitwarden.com/public/members" -H  "accept: text/plain" -H  "Authorization: Bearer XXXX"

It looks like it's breaking down in the header portion. Can you point out what I'm missing?

CodePudding user response:

I believe the issue is in the curl options not being set in separate variables, so the below should resolve the errors:

#!/bin/bash

CURL="/usr/bin/curl"
CURLARGS="-X GET"
BITHTTP="https://api.bitwarden.com/public/members"
header="-H"
accept="accept:text/plain"
auth="Authorization:Bearer XXXX"

$CURL $CURLARGS $BITHTTP $header "$accept" $header "$auth" > /tmp/bitmembers

This answer seems like a good approach to send variables as options to the main curl command. Example:

CURL_OPTIONS=( -X GET "$BITHTTP" -H "$accept" -H "$auth" )
$CURL "${CURL_OPTIONS[@]}"
  • Related