0

I am trying to post a file to an API, which works perfectly under Linux using this curl command:

curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'Authorization: Token foo_bar' -F content=@'Filename.ending'  'http://test.test.com/api/upload'

I don't get how to work with the 'F' Parameter in curl on the Invoke-RestMethod/Invoke-WebRequest command. I tried:

Invoke-WebRequest -Uri $URL -Headers @{Authorization = 'Token '+foo_bar} -Method POST -ContentType 'multipart/form-data'

I tried adding -F 'C:\Users\Me\Filename.ending' to my command, I also tried using -InFile 'C:\Users\Me\Filename.ending', but I always get a response from the API telling me:

{"error":{"code":"GENERAL_ERROR","message":"missing uploaded file 'Filename.ending'"}}

Which probably suggests it wants the file in a different way. I also tried reading it in by Get-Content 'C:\Users\Me\Filename.ending' but had no success. What am I missing?

xtlc
  • 1,070
  • 1
  • 15
  • 41
  • `-Headers @{Authorization = "Token $foo_bar"; Accept='application/json'}` I don't know if you forgot the `$` in your post, or it's missing in your source code (also, string concatenation is unnecessary). – Maximilian Burszley Sep 20 '17 at 17:14
  • Does the API support REST? – Maximilian Burszley Sep 20 '17 at 17:17
  • Thanks for pointing out that error in my post @TheIncorrigible1. I don't know if the API supports that. Is there a method to get all supported methods from an API? – xtlc Sep 20 '17 at 17:53
  • Perhaps try the API's documentation? – Jacob Colvin Sep 20 '17 at 18:44
  • I have a swagger interface, where I can try commands, but I am not sure on how to continue with the "-F" option of the CURL command. I tried '$Buffer = [System.IO.File]::ReadAllBytes($PATH_TO_FILE)' and adding '-Body $Buffer' which did not work. In the CURL Swagger call I can also see this line: **Content-Disposition: form-data; name="content"; filename="samplecontainer" Content-Type: application/octet-stream** which do not perfectly match my attempts. I also tried `-Body @{content = $Buffer}` without success. – xtlc Sep 20 '17 at 20:01
  • This may be a duplicate of: [powershell invoke-restmethod multipart/form-data](https://stackoverflow.com/questions/36268925/powershell-invoke-restmethod-multipart-form-data). – Persistent13 Sep 20 '17 at 20:39
  • I came across that already @Persistent13. Unfortunately it did not help me :( – xtlc Sep 21 '17 at 15:59
  • @xtlc Could you elaborate on what didn't work for you? – Persistent13 Sep 21 '17 at 16:01
  • no matter how I define my `-Body` or use `-InFile` I always get the same (posted above) error message. Even if I just type `$test = 'foo'` and use it in the `-Body` argument, the error message stays the same. So I feel it is not related to the upload. I am just missing a keyword or a method or using it wrong. – xtlc Sep 21 '17 at 16:48
  • I was able to get around the problem, by providing much more input in the `$body` variable: `$boundary = [System.Guid]::NewGuid().ToString() $body = "--$boundary", "Content-Disposition: form-data; name=`"content`"; filename=`"samplecontainer`"", "Content-Type: application/octet-stream$LF", $fileEnc, "--$boundary--$LF"` – xtlc Sep 27 '17 at 14:40

0 Answers0