You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document New -Form Parameter for Web Cmdlets (#2233)
* Add example to Invoke-RestMethod
* Document -Form Feature in Invoke-RestMethod
* Add Simplified Mulitpart/Form-Data example to Invoke-WebRequest
* Document -Form Feature on Invoke-WebRequest
Some REST APIs support pagination via Relation Links per [RFC5988](https://tools.ietf.org/html/rfc5988#page-6). Instead of parsing the header to get the URL for the next page, you can have the cmdlet do this for you. This example returns the first two pages of issues from the PowerShell GitHub repository
138
138
139
+
### Example 4: Simplified Multipart/Form-Data Submission
140
+
```powershell
141
+
$Uri = 'https://api.contoso.com/v2/profile'
142
+
$Form = @{
143
+
firstName = 'John'
144
+
lastName = 'Doe'
145
+
email = 'john.doe@contoso.com'
146
+
avatar = Get-Item -Path 'c:\Pictures\jdoe.png'
147
+
birthday = '1980-10-15'
148
+
hobbies = 'Hiking','Fishing','Jogging'
149
+
}
150
+
$Result = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form
151
+
```
152
+
153
+
Some APIs require `multipart/form-data` submissions to upload files and mixed content.
154
+
This example demonstrates updating a user profile.
155
+
The profile form requires these fields:
156
+
`firstName`, `lastName`, `email`, `avatar`, `birthday`, and `hobbies`.
157
+
The API is expecting an image for the user profile pic to be supplied in the `avatar` field.
158
+
The API will also accept multiple `hobbies` entries to be submitted in the same form.
159
+
160
+
When creating the `$Form` HashTable, the key names are used as form field names.
161
+
By default, the values of the HashTable will be converted to strings.
162
+
If a `System.IO.FileInfo` value is present, the file contents will be submitted.
163
+
If a collection such as arrays or lists are present,
164
+
the form field will be submitted will be submitted multiple times.
165
+
166
+
By using `Get-Item` on the `avatar` key, the `FileInfo` object will be set as the value.
167
+
The result is that the image data for `jdoe.png` will be submitted.
168
+
169
+
By supplying a list to the `hobbies` key,
170
+
the `hobbies` field will be present in the submissions
171
+
once for each list item.
172
+
139
173
## Parameters
140
174
141
175
### -AllowUnencryptedAuthentication
@@ -350,6 +384,60 @@ Accept pipeline input: False
350
384
Accept wildcard characters: False
351
385
```
352
386
387
+
### -Form
388
+
Converts a dictionary to a `multipart/form-data` submission.
389
+
`-Form`may not be used with `-Body`.
390
+
If `-ContentType` will be ignored.
391
+
392
+
The keys of the dictionary will be used as the form field names.
393
+
By default, form values will be converted to string values.
394
+
395
+
If the value is a `System.IO.FileInfo` object,
396
+
then the binary file contents will be submitted.
397
+
The name of the file will be submitted as the `filename`.
398
+
The MIME will be set as `application/octet-stream`.
399
+
`Get-Item`can be used to simplify supplying the `System.IO.FileInfo` object.
This example uses the `Invoke-WebRequest` cmdlet upload a file as a `multipart/form-data` submission. The file `c:\document.txt` will be submitted as the form field `document` with the `Content-Type` of `text/plain`.
153
153
154
+
### Example 5: Simplified Multipart/Form-Data Submission
155
+
```powershell
156
+
$Uri = 'https://api.contoso.com/v2/profile'
157
+
$Form = @{
158
+
firstName = 'John'
159
+
lastName = 'Doe'
160
+
email = 'john.doe@contoso.com'
161
+
avatar = Get-Item -Path 'c:\Pictures\jdoe.png'
162
+
birthday = '1980-10-15'
163
+
hobbies = 'Hiking','Fishing','Jogging'
164
+
}
165
+
$Result = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form
166
+
```
167
+
168
+
Some APIs require `multipart/form-data` submissions to upload files and mixed content.
169
+
This example demonstrates updating a user profile.
170
+
The profile form requires these fields:
171
+
`firstName`, `lastName`, `email`, `avatar`, `birthday`, and `hobbies`.
172
+
The API is expecting an image for the user profile pic to be supplied in the `avatar` field.
173
+
The API will also accept multiple `hobbies` entries to be submitted in the same form.
174
+
175
+
When creating the `$Form` HashTable, the key names are used as form field names.
176
+
By default, the values of the HashTable will be converted to strings.
177
+
If a `System.IO.FileInfo` value is present, the file contents will be submitted.
178
+
If a collection such as arrays or lists are present,
179
+
the form field will be submitted will be submitted multiple times.
180
+
181
+
By using `Get-Item` on the `avatar` key, the `FileInfo` object will be set as the value.
182
+
The result is that the image data for `jdoe.png` will be submitted.
183
+
184
+
By supplying a list to the `hobbies` key,
185
+
the `hobbies` field will be present in the submissions
186
+
once for each list item.
187
+
154
188
## PARAMETERS
155
189
156
190
### -AllowUnencryptedAuthentication
@@ -346,6 +380,60 @@ Accept pipeline input: False
346
380
Accept wildcard characters: False
347
381
```
348
382
383
+
### -Form
384
+
Converts a dictionary to a `multipart/form-data` submission.
385
+
`-Form`may not be used with `-Body`.
386
+
If `-ContentType` will be ignored.
387
+
388
+
The keys of the dictionary will be used as the form field names.
389
+
By default, form values will be converted to string values.
390
+
391
+
If the value is a `System.IO.FileInfo` object,
392
+
then the binary file contents will be submitted.
393
+
The name of the file will be submitted as the `filename`.
394
+
The MIME will be set as `application/octet-stream`.
395
+
`Get-Item`can be used to simplify supplying the `System.IO.FileInfo` object.
0 commit comments