PSMultiPartFormData is a PowerShell module that provides functionality for constructing and managing multipart form data for HTTP requests. This module helps in adding fields, files, and JSON objects to a form data body.
- Create Multipart Form Data: Easily create and manage multipart form data for HTTP requests.
- Add Form Fields: Add standard form fields (e.g., text fields) to the form data with the AddField method.
- Add Files: Add files to the form data using either file content or by specifying a file path. The AddFile method automatically detects the MIME type of the file.
- Add JSON-encoded Objects: Add complex objects as JSON-encoded fields to the form data with the AddObject method.
- Automatic MIME Type Detection: Integrates with the PSMimeTypes module to automatically determine the MIME type of files based on their extension or filename.
- Generate Unique Boundaries: Automatically generates a unique boundary for each multipart form data instance to separate the different parts.
- Generate Multipart Form Data Body: The GetBody method generates the full multipart form data body, ready for use in HTTP requests.
- Get Boundary: The GetBoundary method allows retrieval of the unique boundary used in the multipart form data.
- Reusable Multipart Form Data Objects: You can instantiate new MultiPartFormData objects easily using the New-MultipartFormData function.
- File Import: The Import-FileAsMultipartFormData function allows you to directly import a file from a specified path and add it to the multipart form data.
To use PSMultiPartFormData, import the module into your PowerShell session:
Install-Module "PSMultipartFormData"
Import-Module "PSMultipartFormData"
$formData = New-MultipartFormData
$formData.GetBody()
$formData.GetBoundary()
$formData = Import-FileAsMultipartFormData -FilePath "C:\path\to\file.txt"
$formData.GetBody()
$formData.GetBoundary()
$formData = New-MultipartFormData
$formData.addField('test', 'testificate')
$formData.GetBody()
$formData.GetBoundary()
$requestURI = "https://my.example.api/api/v1/endpoint"
$formData = (New-MultipartFormData).
addField('name', 'John Doe').
addField('age', '55').
addFile('C:\users\john\myfile.pdf')
$request = Invoke-RestMethod -Method Post -Uri $RequestURI -Body $formData.GetBody() -ContentType "multipart/form-data; charset=iso-8859-1; boundary=`"$($formData.GetBoundary())`""
This module relies on:
- PSMimeTypes, A PowerShell module that provides functionality for resolving MIME types from file extensions and filenames. (PSGallery)
Contributions are welcome! If you'd like to improve this module, feel free to submit a pull request or open an issue.
This project is licensed under the Apache 2.0 License. See the LICENSE file for details.
- Kieron Morris (t3hn3rd) - kjm@kieronmorris.me