没有不值得去解决的问题,也没有不值得去学习的技术!

在 PowerShell 中执行 curl 请求:Invoke-WebRequest 无法绑定参数“Headers”

通过 GET 请求对 GitHub GraphQL API 架构运行内省查询。在 PowerShell 中执行 curl 请求:Invoke-WebRequest 无法绑定参数“Headers”

1、通过 GET 请求对 GitHub GraphQL API 架构运行内省查询。在 PowerShell 中执行 curl 请求:Invoke-WebRequest 无法绑定参数“Headers”。如图1

通过 GET 请求对 GitHub GraphQL API 架构运行内省查询。在 PowerShell 中执行 curl 请求:Invoke-WebRequest  无法绑定参数“Headers”
图1
Plaintext

PS E:\wwwroot> curl -H "Authorization: bearer token" https://api.github.com/graphql
Invoke-WebRequest : 无法绑定参数“Headers”。无法将“System.String”类型的“Authorization: bearer ghp_Y
Cwd1mYg2Z8b679QweW2GEmt0bYRob1VxZdH”值转换为“System.Collections.IDictionary”类型。
所在位置 行:1 字符: 9
+ curl -H "Authorization: bearer ghp_YCwd1mYg2Z8b679QweW2GEmt0bYRob1VxZ ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest],ParameterBind
   ingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Co
   mmands.InvokeWebRequestCommand

PS E:\wwwroot>

2、获取 Invoke-WebRequest 的相关帮助文档。执行命令:get-help invoke-webrequest

Plaintext

PS E:\wwwroot> get-help invoke-webrequest                                             
是否要运行 Update-Help?
Update-Help cmdlet 下载 Windows PowerShell 模块的最新帮助文件,并将其安装在你的计算机 上。有关 Update-Help cmdlet
 的详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkId=210614。
[Y] 是(Y)  [N] 否(N)  [S] 暂停(S)  [?] 帮助 (默认值为“Y”): Y

名称
    Invoke-WebRequest

摘要
    Gets content from a web page on the Internet.


语法
    Invoke-WebRequest [-Uri]  [-Body ] [-Certificate ] [-CertificateThumbpri
    nt ] [-ContentType ] [-Credential ] [-DisableKeepAlive] [-Headers ] [-InFile ] [-MaximumRedirection ] [-Metho
    d {Default | Get | Head | Post | Put | Delete | Trace | Options | Merge | Patch}]
     [-OutFile ] [-PassThru] [-Proxy ] [-ProxyCredential <
    System.Management.Automation.PSCredential>] [-ProxyUseDefaultCredentials] [-Sessi
    onVariable ] [-TimeoutSec ] [-TransferEncoding {chun
    ked | compress | deflate | gzip | identity}] [-UseBasicParsing] [-UseDefaultCrede
    ntials] [-UserAgent ] [-WebSession ] []


说明
    The `Invoke-WebRequest` cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web
     page or web service. It parses the response and returns collections of forms, li
    nks, images, and other significant HTML elements.

    This cmdlet was introduced in Windows PowerShell 3.0.

    > [!NOTE] > By default, script code in the web page may be run when the page is b
    eing parsed to populate the > `ParsedHtml` property. Use the `-UseBasicParsing` s
    witch to suppress this.

    > [!IMPORTANT] > The examples in this article reference hosts in the `contoso.com
    ` domain. This is a fictitious > domain used by Microsoft for examples. The examp
    les are designed to show how to use the cmdlets. > However, since the `contoso.co
    m` sites do not exist, the examples do not work. Adapt the examples > to hosts in
     your environment.


相关链接
    Online Version: https://docs.microsoft.com/powershell/module/microsoft.powershell
    .utility/invoke-webrequest?view=powershell-5.1&WT.mc_id=ps-gethelp
    Invoke-RestMethod
    ConvertFrom-Json
    ConvertTo-Json

备注
    若要查看示例,请键入: "get-help Invoke-WebRequest -examples".
    有关详细信息,请键入: "get-help Invoke-WebRequest -detailed".
    若要获取技术信息,请键入: "get-help Invoke-WebRequest -full".
    有关在线帮助,请键入: "get-help Invoke-WebRequest -online"


PS E:\wwwroot>

3、调整语法,使其适应于 PowerShell。curl -Headers @{“Authorization”=”bearer token”} https://api.github.com/graphql 。如图2

调整语法,使其适应于 PowerShell。curl -Headers @{"Authorization"="bearer token"} https://api.github.com/graphql
图2
Plaintext

PS E:\wwwroot> curl -Headers @{"Authorization"="bearer token"} https://api.github.com/graphql


StatusCode        : 200
StatusDescription : OK
Content           : {"data":{"__schema":{"queryType":{"name":"Query"},"mutationType":
                    {"name":"Mutation"},"subscriptionType":null,"types":[{"kind":"INP
                    UT_OBJECT","name":"AbortQueuedMigrationsInput","description":"Aut
                    ogene...
RawContent        : HTTP/1.1 200 OK
                    Vary: Accept, Authorization, Cookie, X-GitHub-OTP,Accept-Encoding
                    , Accept, X-Requested-With
                    X-OAuth-Scopes: admin:enterprise, admin:gpg_key, admin:org, admin
                    :org_hook, admin:public_k...
Forms             : {}
Headers           : {[Vary, Accept, Authorization, Cookie, X-GitHub-OTP,Accept-Encodi
                    ng, Accept, X-Requested-With], [X-OAuth-Scopes, admin:enterprise,
                     admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admi
                    n:repo_hook, delete:packages, delete_repo, gist, notifications, r
                    epo, user, workflow, write:discussion, write:packages], [X-Accept
                    ed-OAuth-Scopes, repo], [github-authentication-token-expiration,
                    2022-03-17 07:57:08 UTC]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : System.__ComObject
RawContentLength  : 2042412



PS E:\wwwroot>

4、决定在 Git Bash 中执行:curl -H “Authorization: bearer token” https://api.github.com/graphql 。可执行成功。如图3

决定在 Git Bash 中执行:curl -H "Authorization: bearer token" https://api.github.com/graphql 。可执行成功
图3

需要长期技术维护或远程问题排查?

我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。

如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:

  • ✅ PHP / Laravel / Yii2 老项目无人维护
  • ✅ Go / Gin 后端接口需要排查或优化
  • ✅ WordPress 网站访问慢、报错或插件冲突
  • ✅ Nginx / MySQL / Redis / Linux 服务器异常
  • ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
  • ✅ 需要长期远程技术支持或兼职维护

更多介绍请查看:关于我 & 合作

微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan