ptpb / pb

pb is a formerly-lightweight pastebin and url shortener
Other
549 stars 52 forks source link

Using pb with PowerShell? #212

Closed tokyoneon closed 6 years ago

tokyoneon commented 6 years ago

@buhman @silverp1

Hey guys, is it possible to upload files using PowerShell instead of cURL? I couldn't figure it out.

> powershell Invoke-RestMethod -Uri 'https://ptpb.pw/~somevanity' -Method POST -InFile .\script.py
buhman commented 6 years ago

According to https://ptpb.pw/a you can't (currently) provide the paste content directly in the request body. IIRC this wasn't the case at some point, but was removed I think, due to some request handling shenanigans related to how werkzeug/flask's api works.


Wow there are a ton of completely awful beyond garbage examples of how to do this in powershell, and exactly zero valid top results for matching "powershell multipart request".

You could do something like this (less code, still borderline awful):

https://stackoverflow.com/questions/36268925/powershell-invoke-restmethod-multipart-form-data

... but afaict, the "correct"+minimum way to do it is something more like this:

$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$fileStream = [System.IO.FileStream]::new(".\script.py", [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = "c"
$fileContent = [System.Net.Http.StreamContent]::new($fileStream)
$fileContent.Headers.ContentDisposition = $fileHeader
$multipartContent.Add($fileContent)

Invoke-WebRequest -Uri "https://ptpb.pw/~somevanity" -Body $multipartContent -Method 'POST'

I got this by reading the upstream documentation for Invoke-RestMethod which references the existance of System.Net.Http.MultipartFormDataContent, which I found example usage for in Invoke-WebRequest.

You'll probably want to make this a function or something that you make available in your shell somehow; I don't know, I don't use powershell at all.

I haven't finished compiling powershell for my platform yet (I didn't need that CPU core anyway, it's ok), but I'm mildly certain this should work with at worst minor tweaks.

tokyoneon commented 6 years ago

I really appreciate the comprehensive answer. Thank you.