How to POST minified js-file and source map to Raygun's source map API endpoint via Windows Powershell script?

JonezS

Posted on
Nov 11 2016

Our company is trying to post my minified filename.js and filename.js.map files to Raygun's Js source center via Powershell script. To do that I applied the instructions provided here: https://raygun.com/docs/workflow/source-maps. However, I couldn't get it working. The server just returns 400 Bad Request when I run the script. What am I missing/doing wrong here?

Here's the powershell script I used to try to send the minified js-file:

$uri = 'https://app.raygun.io/upload/jssymbols/MyAppId?authToken=MyExternalAuthToken'
$file = Get-Item C:\path\to\my\filename.js
$body = @{
    url = 'https://localhost/MyAppName/js/filename.js'
    file = $file
}

try {
    Invoke-RestMethod -Method Post -Uri $uri -Body $body
} catch [Exception] {
    Write-Host $_.Exception
} 

MyAppId and MyExternalAuthToken in the uri are replaced with real ones in the code of course.


Alex

Posted on
Nov 11 2016

Hi,

I believe this should work:

$url = "https://app.raygun.com/upload/jssymbols/[ApplicationIdentifier]?authToken=[MyAuthToken]"

$fileUrl = "http://example.com/myjs.min"
$filePath = "C:\location\of\script.js"
$fileName = "scriptname.js"

$fileBin = [IO.File]::ReadAllBytes($filePath)

$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
$fileEnc = $enc.GetString($fileBin)

$boundary = [System.Guid]::NewGuid().ToString() 

$body = (
    "--$boundary",
  "Content-Disposition: form-data; Content-Type: text/html; name=`"file`"; filename=`"$fileName`"`n",
  $fileEnc,
  "--$boundary",
  "Content-Disposition: form-data; name=`"url`"`n",
  $fileUrl,
  "--$boundary--`n"
) -join "`n"

Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType "multipart/form-data; boundary=`"$boundary`""

You will need to update the [ApplicationIdentifier] and [MyAuthToken] in the URL as well as $fileName, $filePath, $fileUrl with your details.

Unfortunately I couldn't figure out any simpler way to do this with powershell.

I'll take a look at putting this example in the documentation too. Let me know if this works for you or if you have any other questions.

Cheers, Alex


JonezS

Posted on
Nov 12 2016

Great, just tested it and it works perfectly for our purposes. Thank you very much!


Reply