1

I am pretty new to Bash, so forgive my stupid questions.

The goal is to query an API in Bash, currently i am doing so with PS.

I am trying to convert my Powershell script to Bash, but not getting anywhere. Example in PS to GET the specific Model_id from a specific asset ($x).

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$API_Key = Get-Content -Path $ScriptDir\API-key.ps1
$bearer_token = "$API_Key"
$header = @{"Authorization" ="Bearer "+$bearer_token}
$url_GET = "https://test-inventory/api/v1/hardware/bytag/$x"
$Asset = Invoke-RestMethod -Method Get -Uri $url_GET -Headers $header
$Model_id = $Asset.Model_id

Same for making a new asset trough POST

$url_POST = 'https://test-inventory/api/v1/hardware'
$Body = @{ "asset_tag" = "123"; "Another_example" = "dunno"}
Invoke-RestMethod -Method POST -Uri $url_POST -Headers $header -Body $Body

It is pretty simpel code in PS, but for some reason i can't get my head around it in Bash.

zetzke
  • 81
  • 1
  • 7
  • 2
    calling out to [tag:curl] would be one way to do it. – glenn jackman Apr 22 '20 at 17:53
  • True! but i want to keep to code as "Linux basic" as possibel, and curl has to be installed. This is partially because the code is meant for large batches of "new" pc's – zetzke Apr 22 '20 at 18:00
  • 1
    It's a fairly safe assumption to say that most Linux systems will have *either* `curl` or `wget` installed. You could check for the `curl` command, if it exists, use it, otherwise check for the `wget` command, if it exists, use it. – codewario Apr 22 '20 at 18:08
  • Well, you *can* make a raw TCP connection in plain bash (https://stackoverflow.com/q/7765004/7552), but really why would you want to go back to (basically) telnetting in to the web server? – glenn jackman Apr 22 '20 at 19:26
  • @glennjackman thats more the kind of thing i am looking for, thanks man! Its a special kind of usecase i admit :P – zetzke Apr 22 '20 at 19:27

0 Answers0