在 PowerShell 中执行 curl 请求:Invoke-WebRequest 无法绑定参数“Headers”
1、通过 GET 请求对 GitHub GraphQL API 架构运行内省查询。在 PowerShell 中执行 curl 请求:Invoke-WebRequest 无法绑定参数“Headers”。如图1
1 2 3 4 5 6 7 8 9 10 11 12 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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] <System.Uri> [-Body <System.Object>] [-Certificate <Syst em.Security.Cryptography.X509Certificates.X509Certificate>] [-CertificateThumbpri nt <System.String>] [-ContentType <System.String>] [-Credential <System.Managemen t.Automation.PSCredential>] [-DisableKeepAlive] [-Headers <System.Collections.IDi ctionary>] [-InFile <System.String>] [-MaximumRedirection <System.Int32>] [-Metho d { Default | Get | Head | Post | Put | Delete | Trace | Options | Merge | Patch}] [-OutFile <System.String>] [-PassThru] [-Proxy <System.Uri>] [-ProxyCredential < System.Management.Automation.PSCredential>] [-ProxyUseDefaultCredentials] [-Sessi onVariable <System.String>] [-TimeoutSec <System.Int32>] [-TransferEncoding {chun ked | compress | deflate | gzip | identity}] [-UseBasicParsing] [-UseDefaultCrede ntials] [-UserAgent <System.String>] [-WebSession <Microsoft.PowerShell.Commands. WebRequestSession>] [<CommonParameters>] 说明 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. 相关链接 .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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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
近期评论