GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Extend -SkipHeaderValidation to include -UserAgent to support non-standard User-Agent headers by markekraus · Pull Request #4479 · PowerShell/PowerShell

Conversation

@markekraus

@markekraus Mark Kraus (markekraus) commented Aug 3, 2017

Copy link
Copy Markdown
Contributor

Synopsis

Extends #4085 to include the -UserAgent parameter supplied User-Agent header.

Description

Some API's, such as Reddit require special User-Agent headers to identify application consumers of the API. Currently, Invoke-WebRequest and Invoke-RestMethod are unable to properly use non-compliant User-Agent headers. This extends the behavior of the -SkipHeaderValidation switch parameter to include the User-Agent header supplied by the -UserAgent parameter.

Test Code

$Uri = 'http://httpbin.org/headers'
Invoke-WebRequest -Uri $Uri -UserAgent 'Invalid:Agent' -SkipHeaderValidation
Invoke-RestMethod -Uri $Uri -UserAgent 'Invalid:Agent' -SkipHeaderValidation

Current Behavior:

Invoke-WebRequest : The format of value 'Invalid:Agent' is invalid.
At line:2 char:1
+ Invoke-WebRequest -Uri $Uri -UserAgent 'Invalid:Agent'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], FormatException
    + FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Invoke-RestMethod : The format of value 'Invalid:Agent' is invalid.
At line:3 char:1
+ Invoke-RestMethod -Uri $Uri -UserAgent 'Invalid:Agent'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], FormatException
    + FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

New Behavior


StatusCode        : 200
StatusDescription : OK
Content           : {
                      "headers": {
                        "Connection": "close",
                        "Host": "httpbin.org",
                        "User-Agent": "Invalid:Agent"
                      }
                    }

RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    Date: Thu, 03 Aug 2017 10:25:25 GMT
                    Via: 1.1 vegur
                    Server: meinheld/0.6.1
                    Access-Control-Allow-Origin: *
                    Access-Control-Allow-Credentials: true
                    X-Powered-...
Forms             :
Headers           : {[Connection, System.String[]], [Date, System.String[]], [Via, System.String[]], [Server,
                    System.String[]]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        :
RawContentLength  : 113
RelationLink      : {}


headers : @{Connection=close; Host=httpbin.org; User-Agent=Invalid:Agent}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@markekraus
Add UserAgent to SkipHeaderValidation
1d59bc8
@msftclas

Microsoft Contribution License Agreements (msftclas) commented Aug 3, 2017

Copy link
Copy Markdown

Mark Kraus (@markekraus),
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by Microsoft. We will now review your pull request.
Thanks,
Microsoft Pull Request Bot

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-already-signed label Aug 3, 2017
SteveL-MSFT
Steve Lee (SteveL-MSFT) requested changes Aug 4, 2017
View reviewed changes

@SteveL-MSFT Steve Lee (SteveL-MSFT) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, just one requested change

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 Outdated

try {
if ($Cmdlet -eq 'Invoke-WebRequest') {
$result.Output = Invoke-WebRequest -Uri $Uri -TimeoutSec 5 -UserAgent $UserAgent -SkipHeaderValidation:$SkipHeaderValidation.IsPresent

@SteveL-MSFT Steve Lee (SteveL-MSFT) Aug 4, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of duplicating all the parameters, you should use splatting:

$args = @{Uri=$uri;Timeout=5;UserAgent=$useragent;...}
Invoke-WebRequest @args
Invoke-RestMethod @args

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markekraus Mark Kraus (markekraus) Aug 4, 2017

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used $Params instead of $args to avoid conflicts with the automatic variable.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@markekraus
Add Splatting to ExecuteRequestWithCustomUserAgent
97c3f44
SteveL-MSFT
Steve Lee (SteveL-MSFT) approved these changes Aug 4, 2017
View reviewed changes

@SteveL-MSFT Steve Lee (SteveL-MSFT) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@SteveL-MSFT Steve Lee (SteveL-MSFT) assigned Travis Plunk (TravisEz13) Aug 4, 2017
@TravisEz13
Travis Plunk (TravisEz13) merged commit fc77c79 into PowerShell:master Aug 4, 2017
Mark Kraus (markekraus) added a commit to markekraus/PowerShell that referenced this pull request Aug 6, 2017
@markekraus
Fix Tests from PowerShell#4479
f2234b4
@markekraus Mark Kraus (markekraus) mentioned this pull request Aug 6, 2017
Fix Tests from #4479 #4512
Merged
Aditya Patwardhan (adityapatwardhan) pushed a commit that referenced this pull request Aug 7, 2017
@markekraus @adityapatwardhan
Fix Tests from #4479 (#4512)
e4060ab
* Fix Tests from #4479

* [Feature]
@markekraus
Mark Kraus (markekraus) deleted the UserAgentSkipValidation branch August 25, 2017 00:17
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@markekraus @TravisEz13
Extend -SkipHeaderValidation to include -UserAgent to support non-sta…
2ce3a9a
…ndard User-Agent headers (PowerShell#4479)
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@markekraus @adityapatwardhan
Fix Tests from PowerShell#4479 (PowerShell#4512)
1242b09
* Fix Tests from PowerShell#4479

* [Feature]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@SteveL-MSFT Steve Lee (SteveL-MSFT) SteveL-MSFT approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@TravisEz13 Travis Plunk (TravisEz13)

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@markekraus @msftclas @SteveL-MSFT @TravisEz13
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.