Send Image Attachment to PushOver

Welcome Forums General PowerShell Q&A Send Image Attachment to PushOver

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
3 months ago.

  • Author
    Posts
  • #111148

    Participant
    Points: 0
    Rank: Member

    Using the PushOver app (https://pushover.net) I can easily send a message with powershell.  It supports adding image attachments but this part I haven't been able to figure out.

    $uri = "https://api.pushover.net/1/messages.json"
    
    $parameters = @{
    token = "myapptoken"
    user = "myusertoken"
    message = "the message"
    title = "the title"
    }
    
    Invoke-RestMethod -Uri $uri -Method Post -Body $parameters
    
    

    That will cause the message to appear on my phone.

    However if I look at their FAQs (https://pushover.net/api#attachments)

    It says I need to do the following:

    [other HTTP headers]
    Content-Type: multipart/form-data; boundary=--abcdefg
    
    ----abcdefg
    Content-Disposition: form-data; name="user"
    
    [ your Pushover user key ]
    ----abcdefg
    Content-Disposition: form-data; name="token"
    
    [ your Pushover API token ]
    ----abcdefg
    Content-Disposition: form-data; name="message"
    
    your message here
    ----abcdefg
    Content-Disposition: form-data; name="attachment"; filename="your_image.jpg"
    Content-Type: image/jpeg
    
    [ raw binary data of image file here ]
    ----abcdefg--

    I've tried various solution from bing'ing rest uploads, but none work.  I always get 400 bad request.

    Most recently I tried this with no luck:

    $FilePath = 'c:\users\stephen\pictures\mypic.jpg';
    $Uri = 'https://api.pushover.net/1/messages.json';
    
    function AddData{
    param(
    [string]$Name,
    [string]$Value
    )
    $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
    $stringHeader.Name = $Name
    $StringContent = [System.Net.Http.StringContent]::new($Value)
    $StringContent.Headers.ContentDisposition = $stringHeader
    
    return $StringContent
    }
    
    $multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
    
    $multipartContent.Add((AddData -Name "token" -Value "token"))
    $multipartContent.Add((AddData -Name "user" -Value "usertoken"))
    $multipartContent.Add((AddData -Name "message" -Value "the message"))
    $multipartContent.Add((AddData -Name "title" -Value "the title"))
    
    $multipartFile = $FilePath
    $FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)
    $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
    $fileHeader.Name = "attachment"
    $fileHeader.FileName = 'Jay_Small.jpg'
    $fileContent = [System.Net.Http.StreamContent]::new($FileStream)
    $fileContent.Headers.ContentDisposition = $fileHeader
    $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("image/jpg")
    $multipartContent.Add($fileContent)
    $FileStream.Close()
    
    
    Invoke-WebRequest -Uri $uri -Body $multipartContent -Method 'POST' #-ContentType "multipart/form-data;"
    
    
  • #111310

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    So, the difficulty here is that you're really doing low-level .NET programming, just as someone would do in C#. This isn't "PowerShell" per se; you might have better luck asking on a dev-centric site like StackOverflow.com. Even if you found a working example in C#, it'd be easy to translate that into PowerShell, if that's what you need to do.

The topic ‘Send Image Attachment to PushOver’ is closed to new replies.