如果正文参数以“@"开头,则发出 PowerShell POST 请求 [英] Making a PowerShell POST request if a body param starts with '@'

查看:41
本文介绍了如果正文参数以“@"开头,则发出 PowerShell POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 PowerShell 中发出 POST 请求.以下是 Postman 中的正文详细信息.

I want to make a POST request in PowerShell. Following is the body details in Postman.

{
  "@type":"login",
  "username":"xxx@gmail.com",
  "password":"yyy"
}

如何在 PowerShell 中传递它?

How do I pass this in PowerShell?

推荐答案

您应该能够执行以下操作:

You should be able to do the following:

$params = @{"@type"="login";
 "username"="xxx@gmail.com";
 "password"="yyy";
}

Invoke-WebRequest -Uri http://foobar.com/endpoint -Method POST -Body $params

这会将帖子作为正文发送.但是 - 如果您想将此作为 Json 发布,您可能需要明确.要将其作为 JSON 发布,您可以指定 ContentType 并使用

This will send the post as the body. However - if you want to post this as a Json you might want to be explicit. To post this as a JSON you can specify the ContentType and convert the body to Json by using

Invoke-WebRequest -Uri http://foobar.com/endpoint -Method POST -Body ($params|ConvertTo-Json) -ContentType "application/json"

额外:您还可以使用 Invoke-RestMethod 来处理 JSON 和 REST api(这将为您节省一些用于反序列化的额外行)

Extra: You can also use the Invoke-RestMethod for dealing with JSON and REST apis (which will save you some extra lines for de-serializing)

这篇关于如果正文参数以“@"开头,则发出 PowerShell POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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