如何使用PowerShell捕获API请求的输出值

问题描述

我目前正在使用powershell进行一些基本的API REST测试。

但是,我在捕获特定的输出数据时遇到了问题

例如:

$bearer = Invoke-RestMethod -Method POST -Body $body -uri "https://api.yourwebsite.com/oauth/token"

Output:

access_token
------------
{longtokenhere}

但是与标题一起使用时

$header = @{Authorization = "Bearer "+$bearer}

Output is:

Name                           Value
----                           -----
Authorization                  Bearer @{access_token={longtokenhere}}

我想知道如何删除“ @ {access_token =}”部分,以便仅使用{longtokehere}?

解决方法

我做了一些测试,结果证明我可以直接调出access_token输出:

当我做这部分的时候:

$bearer = Invoke-RestMethod -Method POST -Body $body -uri "https://api.yourwebsite.com/oauth/token"

Output:

access_token
------------
{longtokenhere}

我只是简单地使用了这个:

$bearer.access_token

我仅获得令牌的直接输出:

{longtokenhere}

我的最终命令对此起作用:

Invoke-RestMethod -Method GET -Header @{Authorization = "Bearer "+$bearer.access_token} -ContentType "application/json" -uri "https://api.yourwebsite.com/release/releaseID"

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...