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

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

问题描述

我想使用 Powershell 向我的 Vitalist.com 帐户发布新操作.Vitalist API 文档位于此处.
我已经在 Powershell 中尝试过 HttpWebResponse,但我遗漏了一些东西.任何指针表示赞赏.

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.

谢谢.

推荐答案

我对 Vitalis 一无所知,但要执行 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();
  }
}

如果你传递一些数据,像这样对它们进行 urlencode:

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 POST 到 Vitalist.com 的 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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