【问题标题】:convert simple curl data request to powershell invoke-webrequest将简单的 curl 数据请求转换为 powershell invoke-webrequest
【发布时间】:2017-12-12 22:35:10
【问题描述】:

将一个非常简单的 Plaid API curl 数据请求转换为一个 powershell invoke-webrequest

什么有效:

    curl -X POST https://tartan.plaid.com/balance \
  -d client_id=client-id-value \
  -d secret=secret-value \
  -d access_token=access-token-value

我在 Powershell 中的尝试失败

#test powershell api call

$hash = @{  client_id    = "client-id-value";
            secret = "secret-value"
            access_token = "access-token-value"
            }

$JSON = $hash | convertto-json

#Invoke-WebRequest -uri "https://tartan.plaid.com/balance" -Method POST -Body $JSON

这会返回格子错误 1100(缺少客户端 ID),因此我知道某些 API 功能正在运行,但它没有正确解析输入数据。

我最大的误解是如何将“curl -data”值转换为正确的 Powershell 参数。

这是一个标题值,正文值吗?等等。

【问题讨论】:

  • 跳过convertto-json 步骤
  • 更改为以下内容似乎有效,但我知道我的访问令牌已过期。 $post_values = @{client_id='value';secret='value2';access_token='value3'} Invoke-WebRequest -Uri https://tartan.plaid.com/balance -Method POST -Body $post_values

标签: json powershell curl


【解决方案1】:

除非目标网址要求 POST 正文为 JSON 格式,否则请完全跳过 ConvertTo-JSON 步骤。

When the chosen HTTP method is POST, Invoke-WebRequest will automatically take all the keys in the hashtable supplied to the -Body parameter and construct a body payload similar to that of curl -d:

$POSTParams = @{
    client_id    = "client-id-value"
    secret       = "secret-value"
    access_token = "access-token-value"
}

Invoke-WebRequest -Uri "https://tartan.plaid.com/balance" -Method POST -Body $POSTParams

【讨论】:

    猜你喜欢
    • 2018-02-02
    • 2020-02-28
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2019-01-02
    • 2020-03-17
    • 1970-01-01
    • 2020-10-14
    相关资源
    最近更新 更多