-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add Multipart Support to Web Cmdlets #4782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@markekraus, |
CI Rerun count: I
72a3636 to
0796005
Compare
| else if (content is MultipartFormDataContent) | ||
| { | ||
| WebSession.ContentHeaders.Clear(); | ||
| MultipartFormDataContent multipartFormDataContent = content as MultipartFormDataContent; |
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.
We should exclude double casting. Here it is better use C# 7.0 pattern:
else if (content is MultipartFormDataContent multipartFormDataContent ) 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.
Awesome. I will do that.
| if (request == null) | ||
| throw new ArgumentNullException("request"); | ||
| if (multipartContent == null) | ||
| throw new ArgumentNullException("multipartContent"); |
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.
We should use curly braces:
if ( ... )
{
...
}or single line operator.
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.
I can do this, but it will not match the surrounding code.
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.
I believe we should use approved patterns for new code.
You can wait a view of other maintainers.
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.
I agreed with @iSazonov. The new code is somewhat isolated as it's in its own method, so I think it's fine to use the more preferred style.
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.
@daxian-dbw Awesome! format is already corrected in the most recent commit for this PR.
| } | ||
| public ActionResult Index() | ||
| { | ||
|
|
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.
Please remove extra line.
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.
will do
| } | ||
|
|
||
| Context "Multipart/form-data Tests" { | ||
| It "Verifies Invoke-WebRequest Supports Multipart String Values" { |
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.
We use the same Context header 😕
I believe we should add cmdlet name Context "Invoke-WebRequest Multipart/form-data Tests"
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.
Yes, because they are under different describe blocks. We did the same thing with HTTPS
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.
I think it's somewhat confusing, but we can leave it for Context.
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.
For clarity, would you like me to change it or leave it as is?
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.
Leave it as is.
| } | ||
|
|
||
| Context "Multipart/form-data Tests" { | ||
| It "Verifies Invoke-RestMethod Supports Multipart String Values" { |
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.
Context "Invoke-RestMethod Multipart/form-data Tests"
daxian-dbw
left a comment
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.
@markekraus Thank you for the good work! Could you please take a look at the 2 comments I left?
| } | ||
| else if (content is MultipartFormDataContent multipartFormDataContent) | ||
| { | ||
| WebSession.ContentHeaders.Clear(); |
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.
WebSession.ContentHeader is set at line 325 when ContentType is specified. Is it OK to ignore that as well?
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.
Yes, ContentType must be set by the MultipartFormDataContent object as it controls the boundary in the Content-Type header used to distinguish between the various fields supplied. Documentation will probably need to make clear that -ContentType will be ignored when a MultipartFormDataContent object is supplied. It doesn't make sense to use any other Content-Type header when doing multipart/form-data anyway. If this isn't done here, the Foreach on line 421 throws. when Content is set with a MultipartFormDataContent it overwrites all the content headers, so trying to add content headers again causes an exception about ContentType already existing.
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 clarification. I updated the PR description to capture your words.
Then there is a potential problem at line 239
Nevermind, GetRequest happens before FillRequestStream, so we are good. Updated the PR description to mention -Header and -WebSession will be ignored as well.
| $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("text/plain") | ||
| $multipartContent.Add($fileContent) | ||
| } | ||
| # unary comma required |
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.
nit: can you please also mention why it's required -- $multipartContent will be unwrapped/enumerated otherwise.
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.
fixed
daxian-dbw
left a comment
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
|
@markekraus Thanks again for the good work! |
|
@daxian-dbw For documentation, when is that handled? Should I submit a PR/Issue to PowerShell-Docs now or does it need to wait for the next beta release? |
|
Yes, you are welcome to submit a PR to PowerShell-Docs now :)
Please see https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#contributing-to-documentation for more details |
Summary
Partially implements #2112
System.Net.Http.MultipartFormDataContentas a possible type for-Body/Multipart/test to WebListenerThis allows for the user to create their own
Http.MultipartFormDataContentobject and submit it. Sincemultipart/form-datasubmissions are highly flexible, adding direct support for it to the CmdLets may over-complicate the command parameters and a limited implementation would not address the broad scope of use cases. This at least allows the user to submit multipart forms using the Web Cmdlets and not have to manage their ownHttpClient. Once this is introduced, limited multipart implementations can be expanded to use the code in this PR.Documentation Needed
Note that documentation will probably need to make clear that
-ContentTypewill be ignored when aMultipartFormDataContentobject is supplied to-Body. Similarly, content headers specified by-Headersand-WebSessionwill be ignored too.ContentTypemust be set by theMultipartFormDataContentobject as it controls theboundaryin theContent-Typeheader used to distinguish between the various fields supplied. It doesn't make sense to use any otherContent-Typeheader when doingmultipart/form-dataanyway. If this isn't done here, the Foreach on line 421 throws. whenContentis set with aMultipartFormDataContentit overwrites all the content headers, so trying to add content headers again causes an exception aboutContentTypealready exists.