Cannot upload a file through the API with the synchronous parameter


(Peter Wells) #1

I have been trying to add uploaded files through the API using PowerShell. The call appears to work correctly when I use it to upload an image file, but if I try the same call with a synchronous=1 parameter added, it returns with a 500 error.

Based on a previous topic, I had hoped to use the synchronous feature to get the file url.


(Régis Hanol) #2

Can you share your PowerShell code? Or at least the HTTP request you’re making to upload a file?


(Peter Wells) #3

The code looks like this:

$contentType=$r.BaseResponse.ContentType
$content=Get-Content $fileOut
$boundary=[system.Guid]::NewGuid().ToString()
$clientID=[system.Guid]::NewGuid().ToString()
$body=(
"--$boundary",
'Content-Disposition: form-data; name="type"',
'',
"image",
"--$boundary",
"Content-Disposition: form-data; name=""files[]"" filename=$imageName",
"Content-Type: ""$contentType""",
'',
"$content",
"--$boundary--"
) -join "`r`n"
$tempURL=$urlTo+"uploads.json"+$loginTo+"&client_id=$clientID&synchronous=1"
$resp=Invoke-WebRequest $tempURL -Method Post -ContentType "multipart/form-data; boundary=$boundary" -Body $body -Verbose

$urlTo is the url for the site, $loginTo is the APIkey and username. The $r at the top is the response from downloading the image from another site to a file called $fileOut


(Régis Hanol) #4

Code looks correct. What does your.discourse/logs show when you get the 500?


(Peter Wells) #5
/var/www/discourse/app/controllers/uploads_controller.rb:68:in `create_upload'
/var/www/discourse/app/controllers/uploads_controller.rb:23:in `block in create'
/var/www/discourse/lib/scheduler/defer.rb:68:in `call'
/var/www/discourse/lib/scheduler/defer.rb:68:in `do_work'
/var/www/discourse/lib/scheduler/defer.rb:46:in `do_all_work'
/var/www/discourse/lib/scheduler/defer.rb:86:in `process_client'
/var/www/discourse/lib/middleware/unicorn_oobgc.rb:95:in `process_client'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/unicorn-5.0.1/lib/unicorn/http_server.rb:658:in `worker_loop'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/unicorn-5.0.1/lib/unicorn/http_server.rb:508:in `spawn_missing_workers'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/unicorn-5.0.1/lib/unicorn/http_server.rb:132:in `start'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/unicorn-5.0.1/bin/unicorn:126:in `<top (required)>'
/var/www/discourse/vendor/bundle/ruby/2.0.0/bin/unicorn:23:in `load'
/var/www/discourse/vendor/bundle/ruby/2.0.0/bin/unicorn:23:in `<main>'

error from powershell:

[VERBOSE] POST http://website/uploads.json?api_key=efa2a73865db03e8d9f56f71f63758f4619f4edc758bc76791935f74d7095533&api_username=Peter&client_id=49124637-bef0-42e1-b5a3-65d477f56afa&synchronous=1 with 7709-byte payload
[ERROR] Invoke-WebRequest : The remote server returned an error: (500) Internal Server Error.
[ERROR] At C:\Build\DDM\FCRMPS\Discourse.ps1:224 char:15
[ERROR] +                                 $resp=Invoke-WebRequest $tempURL -Method Post -ContentType "multipart/fo ...
[ERROR] +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
[ERROR]     + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
[ERROR]

(Adam Beers) #6

Were you able to figure this out? I’m having the same issue.