如何发布通过PowerShell的Vitalist.com的API? [英] How to POST to Vitalist.com's API via Powershell?

查看:259
本文介绍了如何发布通过PowerShell的Vitalist.com的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢四处使用PowerShell来发布新的动作,我Vitalist.com帐户。
生命哲学的API文档是这里。结果
我试过HttpWebResponse在PowerShell中,但我失去了一些东西。任何指针是AP preciated。

I'd liketo use Powershell to post a new action to my Vitalist.com account. The Vitalist API documentation is here.
I've tried HttpWebResponse in Powershell but I'm missing something. Any pointers are appreciated.

感谢。

推荐答案

我不知道维塔利斯什么,但执行HTTP POST你可以使用这个功能:

I don't know anything about Vitalis, but to execute HTTP POST you can use this function:

function Execute-HttpPost
{
  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();
  }
}

如果您通过一些数据,urlen code他们是这样的:

If you pass some data, urlencode them like this:

add-type -AssemblyName System.Web
[system.Web.Httputility]::UrlEncode($data)

只是一个猜测 - 或许这样的事情可以工作

Just a guess - maybe something like this could work?

$d = [system.Web.Httputility]::UrlEncode("<request><actions><action><body>some body</body></action></actions></request>")
Execute-HttpPost -url 'http://www.vitalist.com/services/api/actions.xml' -data $d -credentials (Get-Credential)

这篇关于如何发布通过PowerShell的Vitalist.com的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆