Title: Using Invoke-WebRequest POST to upload a file is broken · Issue #23843 · PowerShell/PowerShell · GitHub
Open Graph Title: Using Invoke-WebRequest POST to upload a file is broken · Issue #23843 · PowerShell/PowerShell
X Title: Using Invoke-WebRequest POST to upload a file is broken · Issue #23843 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/23843
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Using Invoke-WebRequest POST to upload a file is broken","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://learn.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\nI have a PowerShell module that uploads a zip file of API documentation to a CGI-based website that has been working untouched since 2019. Unfortunately, when I upgraded our build nodes to 7.4.2, it broke the upload to this website. This might be hard to reproduce because I don't know how much of the behavior depends on the CGI website.\r\n\r\n1. Construct the IWR args and call it like so:\r\n\r\n```pwsh\r\n$uri = \"https://api.is.acme.com/cgi-bin/org/of_apps/apiManager/apiManager.cgi\"\r\n\r\n$form = [ordered]@{\r\n USERNAME = $UserName\r\n API_KEY = $ApiKey\r\n APPLICATION_NAME = $AppName\r\n APPLICATION_VERSION = $Version\r\n APPLICATION_VERSION_ALIAS = $VersionAlias\r\n DELETE_EXISTING_VERSION = $AllowClobber ? \"true\" : \"false\"\r\n ROBOT = \"true\"\r\n UPLOADED_FILE = Get-Item $Path # this is a zip file\r\n}\r\n\r\nInvoke-WebRequest -Uri $uri -Method POST -Form $form -SkipCertificateCheck\r\n```\r\nThis silently fails (returns a 200 status code). The file does not get uploaded. When I capture this with Fiddler, this is what I see for the request headers:\r\n\u003cimg width=\"772\" alt=\"image\" src=\"https://github.com/PowerShell/PowerShell/assets/103770981/358a06d1-bd21-47a4-a4e5-e14976791aaa\"\u003e\r\n\r\nWhat is up with the uploaded filename being specified TWICE? And what's with that second funky filename value `utf-8''Klm...`? **I suspect this might be the problem.** It's also interesting that curl quotes the field names e.g. `\"UPLOADED_FILE\"` whereas 7.4.2 does not. Ditto for the filename value.\r\n\r\nNow if I try this with CURL (on Windows) - surprise, it works. :🤦♂️\r\n```pwsh\r\ncurl --insecure -F USERNAME=$username -F API_KEY=$apiDocsKey -F APPLICATION_NAME=\"KAL License Management Client API - Cplusplus\" -F APPLICATION_VERSION=7.4.0-develop -F APPLICATION_VERSION_ALIAS=LATEST-develop -F UPLOADED_FILE=@C:\\Temp\\KlmCppReference.zip -F ROBOT=true https://api.is.keysight.com/cgi-bin/org/of_apps/apiManager/apiManager.cgi -x 127.0.0.1:8888\r\n```\r\nThis results in these headers:\r\n\r\n\r\n\r\n### Expected behavior\r\n\r\n```console\r\nI expect that using `$form[\"UPLOADED_FILE\"] = Get-Item $zipPath` should continue to work and produce the correct multi-part form data - particularly the filename.\r\n```\r\n\r\n\r\n### Actual behavior\r\n\r\n```console\r\nThe CGI website does not accept the uploaded file ... unless I use `curl`.\r\n```\r\n\r\n\r\n### Error details\r\n\r\n```console\r\nThere is no error from Invoke-WebRequest. Even the HTTP status code is 200 in both failed \u0026 working cases. The difference is that when it fails, I get the home HTML as the raw content. When it works I get the somewhat cryptic content:\r\n\r\n1\r\n1\r\n0\r\n\r\n🤷♂️ I didn't write this CGI website.\r\n```\r\n\r\n\r\n### Environment data\r\n\r\n```powershell\r\nName Value\r\n---- -----\r\nPSVersion 7.4.2\r\nPSEdition Core\r\nGitCommitId 7.4.2\r\nOS Microsoft Windows 10.0.22631\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n\r\n\r\n### Visuals\r\n\r\nBad PowerShell POST request headers:\r\n\u003cimg width=\"772\" alt=\"image\" src=\"https://github.com/PowerShell/PowerShell/assets/103770981/799fa3e0-21d4-4be7-8ff5-1caea8677cf0\"\u003e\r\n\r\nWorking curl POST request headers:\r\n\r\n","author":{"url":"https://github.com/rkeithhill-keysight","@type":"Person","name":"rkeithhill-keysight"},"datePublished":"2024-05-25T00:05:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":35},"url":"https://github.com/23843/PowerShell/issues/23843"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:eea51d1e-a00a-352e-1a9d-9e9c28371f02 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EA74:206F88:2D14913:3D63CEF:6A5494C1 |
| html-safe-nonce | 067652f14cf6a72772d4b02d0c65b0372cd88fc46471fdd32c383aff88905860 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTc0OjIwNkY4ODoyRDE0OTEzOjNENjNDRUY6NkE1NDk0QzEiLCJ2aXNpdG9yX2lkIjoiMTc4MTU0NDg5MzA3NzYzOTA1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4de773194cd0c93494418b22ad1a4e262ec521e9804657e54ee847a26d4fed6e |
| hovercard-subject-tag | issue:2316423139 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/23843/issue_layout |
| twitter:image | https://opengraph.githubassets.com/74160a7d8fb0be555ecaa8c68064db927f76cfec590d2784894e02e5e270239c/PowerShell/PowerShell/issues/23843 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/74160a7d8fb0be555ecaa8c68064db927f76cfec590d2784894e02e5e270239c/PowerShell/PowerShell/issues/23843 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rkeithhill-keysight |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f8cfff6384f62259baea531c7f58bc7040f8dd92 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width