Add Multipart Support to Web Cmdlets#4782
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.
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.
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.
We should use curly braces:
if ( ... )
{
...
}or single line operator.
There was a problem hiding this comment.
I can do this, but it will not match the surrounding code.
There was a problem hiding this comment.
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.
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.
@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.
Please remove extra line.
| } | ||
|
|
||
| Context "Multipart/form-data Tests" { | ||
| It "Verifies Invoke-WebRequest Supports Multipart String Values" { |
There was a problem hiding this comment.
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.
Yes, because they are under different describe blocks. We did the same thing with HTTPS
There was a problem hiding this comment.
I think it's somewhat confusing, but we can leave it for Context.
There was a problem hiding this comment.
For clarity, would you like me to change it or leave it as is?
| } | ||
|
|
||
| Context "Multipart/form-data Tests" { | ||
| It "Verifies Invoke-RestMethod Supports Multipart String Values" { |
There was a problem hiding this comment.
Context "Invoke-RestMethod Multipart/form-data Tests"
daxian-dbw
left a comment
There was a problem hiding this comment.
@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.
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.
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.
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.
nit: can you please also mention why it's required -- $multipartContent will be unwrapped/enumerated otherwise.
|
@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.