# Cannot upload a file through the API with the synchronous parameter

**URL:** https://meta.discourse.org/t/cannot-upload-a-file-through-the-api-with-the-synchronous-parameter/43799
**Category:** Development
**Tags:** rest-api
**Created:** [May 6, 2016, 2:33pm UTC](https://meta.discourse.org/t/cannot-upload-a-file-through-the-api-with-the-synchronous-parameter/43799 "2016-05-06T14:33:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![PeterWells](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@PeterWells](https://meta.discourse.org/u/PeterWells)
#### Post date: [May 6, 2016, 2:33pm UTC](https://meta.discourse.org/t/cannot-upload-a-file-through-the-api-with-the-synchronous-parameter/43799/1 "2016-05-06T14:33:05Z")

</div>

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](https://meta.discourse.org/t/using-the-discourse-api-to-post-with-uploaded-files/29833/20), I had hoped to use the synchronous feature to get the file url.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [May 6, 2016, 2:46pm UTC](https://meta.discourse.org/t/cannot-upload-a-file-through-the-api-with-the-synchronous-parameter/43799/2 "2016-05-06T14:46:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![PeterWells](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@PeterWells](https://meta.discourse.org/u/PeterWells)
#### Post date: [May 6, 2016, 3:11pm UTC](https://meta.discourse.org/t/cannot-upload-a-file-through-the-api-with-the-synchronous-parameter/43799/3 "2016-05-06T15:11:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [May 6, 2016, 3:16pm UTC](https://meta.discourse.org/t/cannot-upload-a-file-through-the-api-with-the-synchronous-parameter/43799/4 "2016-05-06T15:16:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![PeterWells](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@PeterWells](https://meta.discourse.org/u/PeterWells)
#### Post date: [May 6, 2016, 3:20pm UTC](https://meta.discourse.org/t/cannot-upload-a-file-through-the-api-with-the-synchronous-parameter/43799/5 "2016-05-06T15:20:30Z")

</div>

```
/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]

```

---

<div class="post-metadata">

### Author: ![adam\_beers](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@adam\_beers](https://meta.discourse.org/u/adam_beers)
#### Post date: [January 6, 2017, 7:33am UTC](https://meta.discourse.org/t/cannot-upload-a-file-through-the-api-with-the-synchronous-parameter/43799/6 "2017-01-06T07:33:44Z")

</div>

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