PowerShell的HTTP POST文件上传的REST API [英] Powershell HTTP POST File Upload for REST api
问题描述
我是新来的PowerShell和有麻烦通过HTTP POST请求发送文件。除了发送/上传文件一切正常使用。这可能使用现有的code?
下面是我的code:
#变量
$ MYFILE =C:\\ sample_file.csv
$器updateURL =http://www.example.com/processor
$ POSTDATA =字段1 =值
$ POSTDATA + =&字段2 =值2
$ POSTDATA + =&MYFILE =+ $ MYFILE #EXECUTE FUNCTION
updateServer -url $器updateURL - 数据$ POSTDATA
功能updateServer {
参数(
[字符串] $ URL = $空,
[字符串] $数据= $空,
[System.Net.NetworkCredential] $凭证= $空,
[字符串] $的contentType =应用/的X WWW的形式urlen codeD,
[字符串] $codePageName =UTF-8,
[字符串] $的userAgent = $空
); 如果($网址 - 和$数据){
[System.Net.WebRequest] $的WebRequest = [System.Net.WebRequest] ::创建($网址);
$ webRequest.ServicePoint.Expect100Continue = $假的;
如果($凭证){
$ webRequest.Credentials = $凭证;
$ WebRequest的preAuthenticate = $真实。
}
$ webRequest.ContentType = $的contentType;
$ webRequest.Method =POST;
如果($的userAgent){
$ webRequest.UserAgent = $的userAgent;
} $ ENC = [System.Text.Encoding] :: GetEncoding($codePageName);
[字节[]] $字节= $ enc.GetBytes($的数据);
$ webRequest.ContentLength = $ bytes.Length;
[的System.IO.Stream] $ reqStream = $ webRequest.GetRequestStream();
$ reqStream.Write($字节,0,$ bytes.Length);
$ reqStream.Flush(); $ RESP = $ webRequest.GetResponse();
$ RS = $ resp.GetResponseStream();
[就是System.IO.StreamReader] $ SR =新对象就是System.IO.StreamReader -ArgumentList $ RS;
$ sr.ReadToEnd();
}
}
两个念头。首先,它似乎您要上传的文件名而不是文件的内容。第二,如果你上传的文件的内容,你可能会需要使用类似 [System.Web.HttpUtility] :: UrlEn code到URL连接code中的数据的POST内()
。此外,检查出我的回答这个相关的SO质疑。
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();
}
}
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.
这篇关于PowerShell的HTTP POST文件上传的REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!