PowerShell Invoke-WebRequest POST上的编码错误

时间:2014-02-06 09:13:08

标签: powershell-v3.0

我正在使用Invoke-WebRequest POST方法发送文本数据。以错误的编码发送文本后。

脚本:

$postData = "žluťoučký kůň úpěl ďábelské ódy"
Invoke-WebRequest -Uri 'http://www.mydomain.com/' -Method Post -Body $postData -ContentType "text/plain; charset=utf-8"

的Fiddler:

POST http://www.mydomain.com/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.2; cs-CZ) WindowsPowerShell/4.0
Content-Type: text/plain; charset=utf-8
Host: www.mydomain.com
Content-Length: 31

zlutouck� kun �pel d�belsk� �dy

被修改

似乎有必要先将文本转换为utf8。 PowerShell ISE默认使用不同的编码。 就我而言,windows-1250。

$text = "žluťoučký kůň úpěl ďábelské ódy"
$postData = [System.Text.Encoding]::UTF8.GetBytes($text)
Invoke-WebRequest -Uri 'http://www.mydomain.com/' -Method Post -Body $postData -ContentType "text/plain; charset=utf-8"

2 个答案:

答案 0 :(得分:5)

这对我有用:

  Message from application: true {"development"=>{"database"=>nil, "adapter"=>"postgresql", "encoding"=>"unicode", "user"=>nil, "password"=>nil, "host"=>nil, "port"=>nil, "pool"=>nil}, "production"=>{"database"=>nil, "adapter"=>"postgresql", "encoding"=>"unicode", "user"=>nil, "password"=>nil, "host"=>nil, "port"=>nil, "pool"=>nil}}, {"GEM_HOME"=>"/opt/rvm/gems/ruby-2.1.2", "SHELL"=>"/usr/sbin/nologin", "IRBRC"=>"/opt/rvm/rubies/ruby-2.1.2/.irbrc", "PYTHONUNBUFFERED"=>"1", "PASSENGER_DEBUG_DIR"=>"/tmp/passenger.spawn-debug.XXXXS4Y1Ah", "MY_RUBY_HOME"=>"/opt/rvm/rubies/ruby-2.1.2", "USER"=>"www-data", "IN_PASSENGER"=>"1", "RACK_ENV"=>"production", "PASSENGER_USE_FEEDBACK_FD"=>"true", "PATH"=>"/opt/rvm/gems/ruby-2.1.2/bin:/opt/rvm/gems/ruby-2.1.2@global/bin:/opt/rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:.", "WSGI_ENV"=>"production", "PWD"=>"/Befective/api", "NODE_PATH"=>"/usr/share/passenger/node", "NODE_ENV"=>"production", "SHLVL"=>"0", "HOME"=>"/var/www", "RAILS_ENV"=>"production", "LOGNAME"=>"www-data", "SERVER_SOFTWARE"=>"nginx/1.10.2 Phusion_Passenger/5.1.2", "GEM_PATH"=>"/opt/rvm/gems/ruby-2.1.2:/opt/rvm/gems/ruby-2.1.2@global", "PASSENGER_APP_ENV"=>"production", "RUBY_VERSION"=>"ruby-2.1.2", "BUNDLER_ORIG_PATH"=>"/opt/rvm/gems/ruby-2.1.2/bin:/opt/rvm/gems/ruby-2.1.2@global/bin:/opt/rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:.", "BUNDLER_ORIG_GEM_PATH"=>"/opt/rvm/gems/ruby-2.1.2:/opt/rvm/gems/ruby-2.1.2@global", "BUNDLE_BIN_PATH"=>"/opt/rvm/gems/ruby-2.1.2/gems/bundler-1.14.6/exe/bundle", "BUNDLE_GEMFILE"=>"/Befective/api/Gemfile", "BUNDLER_VERSION"=>"1.14.6", "RUBYOPT"=>"-rbundler/setup", "RUBYLIB"=>"/opt/rvm/gems/ruby-2.1.2/gems/bundler-1.14.6/lib"} 

添加 charset = utf-8 解决了我的问题,即急性重音字符转换为特殊符号。

答案 1 :(得分:3)

我不得不做两件事来解决问题。编码正文并将字符集添加到标题:

$body = [System.Text.Encoding]::UTF8.GetBytes($body);

$headers = @{
            "Content-Type"="application/json; charset=utf-8";
            "OData-MaxVersion"="4.0";
            "OData-Version"="4.0";
        };

Invoke-WebRequest -Uri "$($odataEndpoint)systemusers($userid)" -Method PATCH -Headers $headers -Body $body -UseDefaultCredentials
相关问题