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

Execute curl Requests in PowerShell: Invoke-WebRequest Cannot Bind the “Headers” Parameter

Run an introspection query against the GitHub GraphQL API schema via a GET request. Executing the curl request in PowerShell results in the error: Invoke-WebRequest cannot bind parameter "Headers"

1. Run an introspection query against the GitHub GraphQL API schema via a GET request. Executing the curl request in PowerShell results in the error: Invoke-WebRequest cannot bind parameter “Headers”. See Figure 1.

Run an introspection query against the GitHub GraphQL API schema via a GET request. Executing the curl request in PowerShell results in the error: Invoke-WebRequest cannot bind parameter "Headers"
Figure 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. Retrieve the relevant help documentation for Invoke-WebRequest. Execute the command: 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. Adjust the syntax to adapt to PowerShell: curl -Headers @{“Authorization”=”bearer token”} https://api.github.com/graphql. See Figure 2.

Adjust the syntax to adapt to PowerShell: curl -Headers @{"Authorization"="bearer token"} https://api.github.com/graphql
Figure 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. Decide to execute in Git Bash: curl -H “Authorization: bearer token” https://api.github.com/graphql. It executes successfully. See Figure 3.

Decide to execute in Git Bash: curl -H "Authorization: bearer token" https://api.github.com/graphql. It executes successfully
Figure 3

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系