1

Problem

I am having a hard time converting a curl call into a Powershell Invoke-RestMethod call as Powershell doesn't really thow the most informative error messages (if any).

Curl call (Ubuntu)

token = "djsakldsakldjaslda"
host = "https://lalala.azuredatabricks.net/"
curl -X POST -H "Authorization: Bearer $(token)" $(host)/api/2.0/clusters/create -d $(cat my_file.json)

Invoke-RestMethod call (Powershell)

$token= "djsakldsakldjaslda"
$host = "https://lalala.azuredatabricks.net/"
Invoke-RestMethod -Method Post -Uri $host/api/2.0/clusters/create -Headers @{"Authorization" = "Bearer " + $token} -Body $(get-content my_file.json -raw | ConvertFrom-Json)

I have various formats for the body, but no matter what I send, I just get some HTML back for a login page. On Ubuntu with Curl everything works perfectly.

NOTE:
The problem seemed to be that PowerShell cannot handle double-"/" as in "https://lalala.azuredatabricks.net//api/2.0/clusters/create".

The strange part is that Invoke-RestMethod gets to the login page, but fails from there.

Esben Eickhardt
  • 3,183
  • 2
  • 35
  • 56
  • Try to add `ConvertTo-Json` after `ConvertFrom-Json` = $(get-content my_file.json -raw | ConvertFrom-Json | ConvertTo-Json) – Shayki Abramczyk Jun 24 '20 at 08:46
  • Lol, that seems to work. OMG, why do I need to use this terrible tool :P – Esben Eickhardt Jun 24 '20 at 08:49
  • I found the error, it seems that unlike Curl the PowerShell method does not accept "//" as in https://lalala.azuredatabricks.net//api/2.0/clusters/create.... Strange that it makes no errors then, but indeed reaches the site... – Esben Eickhardt Jun 24 '20 at 08:51
  • 1
    see my answer for an explanation, you can also try to remove both ` | ConvertFrom-Json | ConvertTo-Json` maybe it will work. check it :) – Shayki Abramczyk Jun 24 '20 at 08:59
  • 1
    @ShaykiAbramczyk Huh? When the file already is JSON, and the OP calls `ConvertFrom-JSON`, but the API expects JSON, then the solution is not calling `ConvertTo-JSON` again, the solution is dropping the `ConvertFrom-JSON`. – Tomalak Jun 24 '20 at 09:01
  • I thought about it but from my experience not always is works, lol. id it worked for you? sometimes the `ConvertTo-Json` create a better format than in the original file – Shayki Abramczyk Jun 24 '20 at 09:03
  • JSON is JSON, the way it is formatted makes no difference at all. – Tomalak Jun 24 '20 at 09:05

2 Answers2

2

Use -InFile to upload a file. Don't forget to set the content type.

Wrapped for legibility (escaping the EOL works as line continuation in PowerShell, it looks funny because StackOverflow syntax highlighting cannot handle it):

Invoke-RestMethod `
    -Method Post `
    -Uri "$host/api/2.0/clusters/create"
    -Headers @{
        Authorization = "Bearer $token"
    } `
    -Infile my_file.json
    -ContentType "application/json"
Tomalak
  • 332,285
  • 67
  • 532
  • 628
1

The body is expected to be JSON format, when you take the file and add the | ConvertFrom-Json the content becomes a PowerShell object.

So, you can remove the | ConvertFrom-Json and it should work :)

Palle Due
  • 5,929
  • 4
  • 17
  • 32
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • 1
    Thanks for the answer. I ended up not using ConvertFrom-Json and ConvertTo-Json, as the real problem was the extra "/" in the URL. If you type that into you answer, I will accept :) – Esben Eickhardt Jun 24 '20 at 09:04