0

I try to upload images using Invoke-RestMethod from powershell

When i try to POST to PHP from remote server i don't receive any answer from PHP.

Here is my code from powershell:

>     function uploadAPI($uri, $file, $api) 
>     {
>         $result = Invoke-RestMethod -Uri $uri -Method Post -InFile $file -ContentType "multipart/form-data"
>         Write-Output $result
>         Break Script
>         
>     }
>     
>     uploadAPI "http://192.168.1.59:8888/api/" "C:\source\italian-landscape-mountains-nature.jpg" "w"

and here is the code from PHP

> print_r([$_FILES, $_POST, $_REQUEST]);
> 
> die;

The response is empty:

> Array (
>     [0] => Array
>         (
>         )
> 
>     [1] => Array
>         (
>         )
> 
>     [2] => Array
>         (
>         )
> 
> )
MiddleCloud
  • 210
  • 1
  • 2
  • 16
  • "The response is empty:" Not it's not. The response is a string (the result of print_r). It looks more like the _request_ is empty (no data sent). Use a tool like Fiddler to see what's actually being sent in the request. Is your file path definitely valid? Also, what you're doing may simply be insufficient (this one of many results from a simple google search of your requirement (send a file via powershell invoke-rest): see https://stackoverflow.com/questions/25075010/upload-multiple-files-from-powershell-script – ADyson Sep 25 '17 at 10:47
  • Yes. It is partial true but you have missed the global variable $_FILES what will capture any file that has been sent to the PHP script.What is empty as well. `print_r` will return a string just when the `return` parameter is set to true otherwise will return the type of var the is passed.So you are telling me that i can't use PowerShell to send Rest information? – MiddleCloud Sep 25 '17 at 12:12
  • "i can't use PowerShell to send Rest information". There's no such thing as "Rest information". REST is just a convention for structuring data and APIs which exchange information over HTTP, it's not a protocol or a standard or anything. What you're doing is just a HTTP request, depsite the silly PowerShell name for the method. Anyway I haven't missed the $_FILES bit - your output clearly shows that $_FILES is empty as well. That's why I asked if your filename was valid. – ADyson Sep 25 '17 at 12:18
  • And I didn't say it was impossible,I suggested that you look at that other link to see why you might need to more than just your current code. There are various solutions given there. The method you're using doesn't, according to those answers, construct the request properly. That's also why I suggested you examine the actual request being made to see if it's valid or not. You seem to have missed my point. – ADyson Sep 25 '17 at 12:19
  • Yes! The file and the path it is valid. I'm very newbie in PowerShell and i don't know if is ok how i made the request towards PHP script. This is the entire script in both sides. I want to make sure that i receive the file in PHP end. – MiddleCloud Sep 25 '17 at 12:35
  • And I'm telling you that according to those answers, how you did it is not ok. I don't know much about powershell either, but I can read and comprehend. You can use one of those scripts instead and see if it's successful. – ADyson Sep 25 '17 at 12:39

0 Answers0