powershell - PowerShell GraphQL突变解析错误

标签 powershell graphql

我正在查询Monday API v2(https://monday.com/developers/v2),我无法使PowerShell查询正常工作,脚本如下:

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
    $url = "https://api.monday.com/v2/"
    $hdr = @{ }
    $hdr.Add("Authorization", "redacted")
    $hdr.Add("Content-Type", "application/json")

    $bodytxt = '{"query":"mutation{change_column_value (board_id: 528033866, item_id: 574335355, column_id: \"status_1\", value: "{\"index\": 1}") {id}}"}'
    $response = Invoke-WebRequest -Uri $url -Headers $hdr -Method Post -body $bodytxt
    Write-Host $response 

而且不断返回
{"errors":[{"message":"Parse error on \": 1}\" (STRING) at [1, 110]","locations":[{"line":1,"column":110}]}],"account_id":5305133}

Postman中的突变查询工作正常
change_column_value (board_id: 528033866, item_id: 574335355, column_id: "status_1", value: "{\"index\": 1}"){id}
}

我曾尝试处理哪些转义的引号,但是我仍然无法使其成功运行。我在这里引用技巧https://www.reddit.com/r/PowerShell/comments/b9jasa/query_graphql_with_powershell/

关于如何解决此错误的任何想法?

编辑:对不起忘了添加通过Powershell正常工作的当前查询(不是突变)
$bodytxt = '{"query":"{  boards (ids: 528033866) {    groups (ids: \"group_title\"){        items () {            id            name       updates () {  body }     column_values () {        id        title        value    text  }        }        title    }    }  }"}'

最佳答案

创建GraphQL查询的最简单方法是

  • HERE-STRINGS创建一个哈希表
  • 使用ConvertTo-Json将其转换为JSON字符串。

  • $bodytxt = @{"query" = @'
      mutation {
        change_column_value(
          board_id: 123123
          item_id: 456456
          column_id: "status1"
          value: "{\"index\":1}"
        ) {
          id
        }
      }
    '@
    } | ConvertTo-Json
    
    @'...'@内的空格是可选的。

    回复this comment

    it should always work in the API call with the GraphQL?



    我认为这取决于服务,但是在大多数情况下,是的。 application/json是请求正文的GraphQL默认内容类型。

    monday.com

    Be sure to use the application/json content type

    关于powershell - PowerShell GraphQL突变解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61960272/

    相关文章:

    linux - 从 PowerShell 运行 Linux 命令

    powershell - Runbook 中的 Azure DSC 配置

    javascript - CORS 阻止 GraphQL Yoga 中的突变

    json - 使用 GitHub api 调用 WebRequest 失败 - "Problems parsing json"

    winforms - 如何使用Powershell和WPF构建动态链接列表?

    powershell - Atom - 无法保存文件 : Permission denied (Windows)

    javascript - 在 graphql 的解析器中获取用户代理访问权限

    graphql - 如何在多种类型上使用 GraphQL 片段

    javascript - GraphQL Mutation 如何将自身与 Type 关联起来

    .net - $ExecutionContext.InvokeCommand.GetCommand() 和 Get-Command 有什么区别?