Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Invoke-RestMethod to upload jpg

Tags:

powershell

I'm trying to use the PowerShell commandlet Invoke-RestMethod to post a picture to a url. Here's my commands:

$usercreds = Get-Credential
$pic = Get-Content \\server\share\pic.jpg
$uri = http://website/sub/destination

Invoke-RestMethod -uri $uri -Method Put -Body $pic -ContentType 'image/jpg' -Credential $usercreds

I get an error: "the file is not a valid image file." I tried using Invoke-WebRequest too, with the same result. The web server isn't ours, and the tech on their side said to use curl, but we don't know how and don't have a Linux box either. Is there something I'm missing? I can open the jpg without issue so it's not corrupt or anything.

I tried this, but the server yelled at me: Using PowerShell Invoke-RestMethod to POST large binary multipart/form-data

Error code:

PS C:\Windows\system32> Invoke-WebRequest -uri $uri -Method Put -Body $pic -ContentType 'image/jpg' -Credential $usercreds
Invoke-WebRequest : {"error":{"message":"the file is not a valid image file"},"data":[],"meta":"error"}
At line:1 char:1
+ Invoke-WebRequest -uri $uri -Method Put -Body $pic -ContentType 'imag ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
like image 991
user3494277 Avatar asked Feb 22 '17 15:02

user3494277


Video Answer


2 Answers

Try using the -Infile Parameter. Get-Content interprets your file an array of strings and just messes things up.

$usercreds = Get-Credential
$picPath = "\\server\share\pic.jpg"
$uri = http://website/sub/destination

Invoke-WebRequest -uri $uri -Method Put -Infile $picPath -ContentType 'image/jpg' -Credential $usercreds
like image 112
TToni Avatar answered Nov 03 '22 02:11

TToni


I've been trying for a day to get this working. My setup was a powershell script that was pushing a screenshot into a nodejs express api endpoint that was saving to the server. The above code almost worked, that is, the endpoint was hit and the file was saved, but the whatever was being saved wasn't the right encoding. I searched high and low for a solution, used multiple frameworks like multer, formidable, busboy etc. Tried various methods that involved building up the body for a multipart form, but with the same results.

What worked in the end for me (in case anyone else is reading) was to send it as base64 data and convert it on the other end because something wasn't going right with the encoding and I couldn't work it out.

Powershell script ($path and $uri are as above, nothing different)

$base64Image = [convert]::ToBase64String((get-content $path -encoding byte))
Invoke-WebRequest -uri $uri -Method Post -Body $base64Image -ContentType "application/base64"

Nodejs Express


app.post("/api/screenshot/", (req,res) => {
    let body = '';
    req.on('data', chunk => {
        body += chunk.toString();
    });
    req.on('end', () => {
      fs.writeFile(__dirname + '/public/images/a.jpg', body,'base64',function(err) {
        if( err ) {
          res.end('not okay');
        } else {
          res.end('ok');
        }
      });
    });
});

Probably more inefficient, but I needed to get something working.

like image 25
Jarrod McGuire Avatar answered Nov 03 '22 02:11

Jarrod McGuire