cURL到PowerShell---data中的双哈希表? [英] cURL to PowerShell - Double hash table in --data?

查看:69
本文介绍了cURL到PowerShell---data中的双哈希表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试(失败了几个小时)将此cURL脚本转换为PowerShell:

I've been trying (and failing for hours) to convert this cURL script into PowerShell:

curl -X POST \
  https://example.net \
  -H 'Authorization: Bearer 1234567890' \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
    "data": {
        "MID": 33,
        "DID": "66666666",
        "CID": 10002,
        "status": "ACTIVE",
        "HID": "11"
    }
}'

我的PowerShell脚本如下所示-我怀疑这与我在$ Body中创建的双哈希表有关但是我真的很茫然。

My PowerShell script looks like this - I suspect it's something to do with the double hash table I've created in $Body but I am really at a loss.:

..为简单起见,脚本被剪掉了

.. script snipped for simplicity

$Body =   @{
    'data'= @{
      'MID'= 33;
      'DID'= "66666666";
      'CID'=10002;
      'status'="ACTIVE";
      'HID'= "11"
    }
}
$CurlArgument = '-X', 'POST',
                'https://example.net',
                '-H', 'Authorization: Bearer 1234567890',
                '-H', 'Content-Type: application/json',
                '-d', 
                $Body
$CURLEXE = 'C:\Windows\System32\curl.exe'
& $CURLEXE @CurlArgument

执行时,我收到以下错误消息:

I get the following error message when it executes:

..."message":"Access not allowed","details":{"5011":"A JSONObject text must begin with '{' at 1 [character 2 line 1]"}}]}

我尝试在$ Body之后添加:

I experimented with adding after $Body:

| ConvertTo-Json

但这会给我这个错误:

Unexpected character ('d' (code 100)): was expecting double-quote to start field name

我的$ CurlArgument变量如下所示(与第一个cURL脚本相同):

My $CurlArgument variable looks like this (which looks the same as the first cURL script):

-X
POST
https://example.net
-H
Authorization: Bearer 1234567890
-H
Content-Type: application/json
-d
{
    "data":  {
                 "CID":  10002,
                 "MID":  33,
                 "HID":  "11",
                 "DID":  "66666666",
                 "status":  "ACTIVE"
             }
}

一如既往,我们将不胜感激。

As always, assistance would be greatly appreciated.

推荐答案


  • 您的 $ Body 变量包含 哈希表 @ {...} ,因此您必须使用 ConvertTo-Json 明确将其转换为JSON。

    • Your $Body variable contains a hashtable (@{ ... }), so you must explicitly convert it to JSON with ConvertTo-Json.

      此外,因为您正在调用外部程序,则必须转义 字符。在JSON字符串中为 \ [1]

      Additionally, because you're calling an external program, you must escape the " chars. in the JSON string as \"[1]:

      $CurlArgument = '-X', 'POST',
                      'https://example.net',
                      '-H', 'Authorization: Bearer 1234567890',
                      '-H', 'Content-Type: application/json',
                      '-d', 
                      (($Body | ConvertTo-Json) -replace '"', '\"')
      






      [1]不幸的是,这个额外的转义要求是很不幸的,但是到目前为止,为了向后兼容,它一直保留着。 ://github.com/PowerShell/PowerShell-Docs/issues/2361 rel = nofollow noreferrer>此GitHub文档问题讲述了整个故事。


      [1] This additional escaping requirement is highly unfortunate, but to date it has been kept around for the sake of backward compatibility. This GitHub docs issue tells the whole story.

      这篇关于cURL到PowerShell---data中的双哈希表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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