Execute curl request in PowerShell: invoke-webrequest cannot bind parameter “headers”
1. Run an introspective query to the GitHub GraphQL API schema through the GET request. Execute a curl request in PowerShell: invoke-webrequest cannot bind the parameter “headers”. as shown in Figure 1
PS E:\wwwroot> curl -h "authorization: bearer token" https://api.github.com/graphql invoke-webrequest : The parameter "headers" cannot be bound. "Authorization: Bearer GHP_Y can't be changed to type "System.String" cwd1myg2z8b679qwew2gemt0byrob1vxzdh" value is converted to "System.Collections.IDictionary" type. Location Line:1 Character: 9 + curl -h "authorization: Bearer GHP_YCWD1Myg2Z8B679Qwew2Gemt0ByRob1VXZ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:)[Invoke-WebRequest], ParameterBind ingException + FullyQualifiedErrorId : canNotConvertArgumentNoMessage, Microsoft.PowerShell.co MANDS.InvokeWebRequestCommand PS e:\wwwroot>
2. Get the relevant help documentation for invoke-webrequest. Execute the command: get-help invoke-webrequest
PS E:\wwwroot> get-help invoke-webrequest
Do you want to run update-help?
The update-help cmdlet downloads the latest help file for the Windows PowerShell module and installs it on your computer. About the update-help cmdlet
For details, see https:/go.microsoft.com/fwlink/?LinkId=210614.
[Y]yes (Y)[N]No (n)[S]Suspension (S)[?]Help (default is "Y"): Y
Name
invoke-webrequest
Summary
Gets content from a web page on the Internet.
Grammar
invoke-webrequest[-Uri]<system.uri>[-Body <System.Object>][-Certificate <Syst
em.security.cryptography.x509certificates.x509certificate>]#atfp_close_translate_span#[-CertificateThumbpri
nt <system.string>]#atfp_close_translate_span#[-ContentType <System.String>][-Credential <System.Managemen
t.automation.pscredential>]#atfp_close_translate_span#[-DisableKeepAlive][-Headers <System.Collections.IDi
ctionary>]#atfp_close_translate_span#[-InFile <System.String>][-MaximumRedirection <System.Int32>][-Metho
d {default | get | head | post | put | delete | trace | options | merge | patch}]#atfp_close_translate_span#
[-OutFile <System.String>][-PassThru][-Proxy <System.Uri>][-ProxyCredential <
system.management.automation.pscredential>]#atfp_close_translate_span#[-ProxyUseDefaultCredentials][-Sessi
onvariable <system.string>]#atfp_close_translate_span#[-TimeoutSec <System.Int32>][-TransferEncoding {chun
KED | COMPRESS | Deflate | Gzip | Identity}]#ATFP_CLOSE_TRANSLATE_SPAN#[-UseBasicParsing][-UseDefaultCrede
NTIALS]#ATFP_CLOSE_Translate_span#[-UserAgent <System.String>][-WebSession <Microsoft.PowerShell.Commands.
WebRequestSession>]#atfp_close_translate_span#[<CommonParameters>]
Explanation
The `Invoke-WebRequest` cmdlet sends HTTP, HTTPS, FTP, and file requests to a web
It parse the response and return 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 supply this.
>[!IMPORTANT]> the examples in this article reference hosts in the `contoso.com
`Domain. This is a fictitious >
Les are designed to show how to use the cmdlets.
M` Sites do not exist, the examples do not work. Adapt the examples > to hosts in
your environment.
Related Links
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell
.utility/invoke-webrequest?view=powershell-5.1&wt.mc_id=ps-gethlp
invoke-restmethod
convertfrom-json
convertto-json
Remark
To see an example, type: "get-help invoke-webrequest -examples".
For details, type: "get-help invoke-webrequest -detailed".
To get technical information, type: "get-help invoke-webrequest -full".
For online help, type: "get-help invoke-webrequest -online"
PS e:\wwwroot>
3. Adjust the syntax to adapt it to PowerShell. curl -headers @{“authorization”=”Bearer token”} https://api.github.com/graphql . as shown in Figure 2
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-Op, 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]#atfp_close_translate_span#,[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]#atfp_close_translate_span#,[X-Accept
ed-oauth-scopes, repo]#atfp_close_translate_span#,[github-authentication-token-expiration,
2022-03-17 07:57:08 UTC]#ATFP_CLOSE_Translate_span#...}
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 . Execution is successful. as shown in Figure 3


