繁体   English   中英

将多头和json输入curl命令转换为powershell

[英]Convert multi header and json input curl command in to powershell

我有以下 curl 命令,我正在尝试将其转换为 powershell,但我不确定如何处理多标头输入和 JSON 输入

非常感谢任何帮助

         curl --location --request POST 'https://anypoint.studio.com/cloudhub/api/v2/applications' \
     --header 'X-ANYPNT-ENV-ID:  4a96abfd4f5237cf1b64' \
     --header 'X-ANYPNT-ORG-ID: bc0d-3b9fd79234ad' \
     --header 'Authorization: Bearer 3esede-a44b-29ab8841b508' \
     --form 'file=@"/C:/Users/pgi/Downloads/eafdc-flow-proxy-v1.2.jar"' \
     --form 'appInfoJson="{
\"domain\": \"testproxy-test\",
\"Version\": {
    \"version\": \"4.4.0\"
},
      \"properties\": {
    \"platform.client_id\": \"8f95qw3sa4b679aaa699cf0f5c6b6\",
    \"secure.key\": \"sYf%NJ7F^y&3lNRH*D$@\",
    \"env\": \"dev\",
    \"platform.client_secret\": \"acF0JHFUEFH8829a481D9c37EF364be7a\"
},
      \"propertiesOptions\": {
    \"secure.key\": {
        \"secure\": true
    },
    \"anypoint.platform.client_secret\": {
        \"secure\": true
    }
},
\"region\": \"us-west-1\",
\"monitoringEnabled\": true,
\"monitoringAutoRestart\": true,
\"workers\": {
    \"amount\": 1,
    \"type\": {
        \"name\": \"Micro\",
        \"weight\": 0.1,
        \"cpu\": \"0.1 vCores\",
        \"memory\": \"500 MB memory\"
    }
},
\"loggingNgEnabled\": true,
\"persistentQueues\": true
  }"' \
 --form 'autoStart="true"'

您可以将curl替换为Invoke-WebRequest并使用ConvertFrom-Json解析 JSON。

$r = Invoke-WebRequest -Uri $uri -Method Post -Body $body
$j = ConvertFrom-Json $r.Content

我没有设置$uri$body的值。 我希望这足以让你开始。

更新

第一反应写得太快了。 您可能需要传递标题和正文。

与 curl 一样,Invoke-Webrequest 功能强大且复杂。 您可以在此处找到在线文档:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.2&WT.mc_id=ps-gethelp

您需要使用 Headers 和 Body 参数:

-标题 <System.Collections.IDictionary>

指定 Web 请求的标头。 输入哈希表或字典。

要设置 UserAgent 标头,请使用 UserAgent 参数。 您不能使用此参数来指定 User-Agent 或 cookie 标头。

当为 Body 提供 MultipartFormDataContent 对象时,会覆盖与内容相关的标头,例如Content-Type

-Body <System.Object>

指定请求的正文。 正文是请求头之后的请求内容。 您还可以通过管道将正文值传递给Invoke-WebRequest

Body 参数可用于指定查询参数列表或指定响应的内容。

当输入是 GET 请求并且正文是IDictionary (通常是哈希表)时,正文将作为查询参数添加到 URI。 对于其他请求类型(如 POST),body 被设置为标准name=value格式的请求 body 的值。

Body 参数也可以接受System.Net.Http.MultipartFormDataContent对象。 这有助于multipart/form-data请求。 当为 Body 提供 MultipartFormDataContent 对象时,提供给 ContentType 、 Headers 或 WebSession 参数的任何与 Content 相关的标头都会被 MultipartFormDataContent 对象的 Content 标头覆盖。 此功能是在 PowerShell 6.0.0 中添加的。

看起来您必须创建一个MultipartFormDataContent对象。 请注意,这是在 PowerShell 版本 6 中添加的,因此您不能使用 Windows 内置的默认版本 5。如果尚未安装,您可能需要安装最新版本的版本 7。(我使用版本 7这很棒。)

因此,对于标题,您将创建如下内容:

$headers = [ordered]@{
    'X-ANYPNT-ENV-ID' = '4a96abfd4f5237cf1b64'
    'X-ANYPNT-ORG-ID' = 'bc0d-3b9fd79234ad'
    'Authorization' = 'Bearer 3esede-a44b-29ab8841b508'
}

然后拨打电话:

$r = Invoke-WebRequest -Uri $uri -Method Post `
    -Headers $headers -Body $myMultipartFormDataContent

我还没有使用MultipartFormDataContent ,所以我会留给你。 我希望这可以为您提供一个良好的起点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM