0

I'm trying to pass an image in power shell replicating this cURL request

curl -u user:apikey -F ‘data=@1234.jpg’
https://denton.gradesfirst.com/api/users/pictures

I authenticate fine but don't know to replicate ‘data=@1234.jpg’ in powershell so the other system will know where the picture is.

$username = "username"
$password = "apikey"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))



Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -uri "https://test.test.test.com/api/users/pictures" -Method Put -InFile "C:\Users\username\Downloads\pic-2017\210569.jpg" -ContentType 'image/jpg'
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
  • I'm fairly sure you can do this by reading the file as raw bytes and uploading them. I don't think your use of `-InFile` is correct as I believe that `Invoke-RestMethod` is expecting `-InFile` to contain your entire request. Try doing this: `$file = [io.file]::ReadAllBytes('C:\myfile.jpg') Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -uri "https://test.test.test.com/api/users/pictures" -Method Put -body $file -ContentType 'image/jpg'` – Ty Savercool Aug 07 '17 at 20:32
  • After further checking, I found this: https://stackoverflow.com/questions/42395638/how-to-use-invoke-restmethod-to-upload-jpg It would suggest that uploading the file's bytes in unnecessary and `-ToFile` should work. Unfortunately the requester never confirmed. Are you getting an error you can post? – Ty Savercool Aug 07 '17 at 21:02
  • I'm not getting an error. Do i need to tag the body – user3821790 Aug 07 '17 at 21:48
  • -Method Put -body data=@$file -ContentType 'image/jpg' – user3821790 Aug 07 '17 at 21:48
  • I getting a 500 error when look at the call from post man it comes back like this ------WebKitFormBoundarypQAyTDIZ4ZsUa2ET Content-Disposition: form-data; name="data"; filename="210569.jpg" Content-Type: image/jpeg – user3821790 Aug 08 '17 at 19:13
  • Power shell looks like this Content=form-data&name=System.Object%5b%5d&filename= – user3821790 Aug 08 '17 at 19:15

1 Answers1

0

This is a common mis-nomer that you must use invoke-restmethod.

This should work for you. If you need to input the creds non-interactively, simply save them to a file and then pull them in to the same $cred variable - the commands remain the same.

$creds = Get-Credential
$url = "https://denton.gradesfirst.com/api/users/pictures"
Invoke-WebRequest -Credential $creds -Method Post -Uri $url -ContentType "application/x-www-form-urlencoded" -InFile "1234.jpg"
Collin Chaffin
  • 872
  • 9
  • 15