0

I am new to Powershell and having trouble sending a file via an HTTP POST request. Everything is working perfectly except for sending/uploading the file. Is this possible using my existing code?

Here is my code:



    # VARIABLES
    $myFile = "c:\sample_file.csv"
    $updateUrl = "http://www.example.com/processor"
    $postData  =  "field1=value1"
    $postData += "&field2=value2"
    $postData += "&myFile=" + $myFile

    # EXECUTE FUNCTION
    updateServer -url $updateUrl -data $postData



    function updateServer {
        param(
            [string]$url = $null,
            [string]$data = $null,
            [System.Net.NetworkCredential]$credentials = $null,
            [string]$contentType = "application/x-www-form-urlencoded",
            [string]$codePageName = "UTF-8",
            [string]$userAgent = $null
        );

        if ( $url -and $data ){
            [System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($url);
            $webRequest.ServicePoint.Expect100Continue = $false;
            if ( $credentials ){
                $webRequest.Credentials = $credentials;
                $webRequest.PreAuthenticate = $true;
            }
            $webRequest.ContentType = $contentType;
            $webRequest.Method = "POST";
            if ( $userAgent ){
                $webRequest.UserAgent = $userAgent;
            }

            $enc = [System.Text.Encoding]::GetEncoding($codePageName);
            [byte[]]$bytes = $enc.GetBytes($data);
            $webRequest.ContentLength = $bytes.Length;
            [System.IO.Stream]$reqStream = $webRequest.GetRequestStream();
            $reqStream.Write($bytes, 0, $bytes.Length);
            $reqStream.Flush();

            $resp = $webRequest.GetResponse();
            $rs = $resp.GetResponseStream();
            [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
            $sr.ReadToEnd();
        }
    }

2
  • What part is not working and what error are you getting Commented Feb 14, 2013 at 1:13
  • The file upload is not working. The server receives all parameters fine except the file itself. Commented Feb 14, 2013 at 4:13

2 Answers 2

2

Two thoughts. First it seems you're uploading the filename but not the file's contents. Second, if you upload the file's contents within the POST you're likely going to need to URL encode the data using something like [System.Web.HttpUtility]::UrlEncode(). Also, check out my answer to this related SO question.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, with the code above I was uploading the filename. I also tried using <pre><code>filecontent=([IO.File]::ReadAllText('c:\file.txt')) </code</pre> and it seems to upload the file contents, but the existing form processor expects a file as normally uploading through an http form.
0

I found the solution to this problem here. I think I may have come across this when I was building my script originally or a snippet of it somewhere else as it is nearly identical to what I have except more thorough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.