Wrong encoding on PowerShell Invoke-WebRequest POST

文章推薦指數: 80 %
投票人數:10人

... ďábelské ódy" Invoke-WebRequest -Uri 'http://www.example.com/' -Method Post -Body $postData -ContentType "text/plain; charset=utf-8". Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams WrongencodingonPowerShellInvoke-WebRequestPOST AskQuestion Asked 8years,8monthsago Modified 4years,2monthsago Viewed 30ktimes 22 I'musingInvoke-WebRequestPOSTmethodtosendantextdata.Aftersendingthetextinawrongencoding. Script: $postData="žluťoučkýkůňúpělďábelskéódy" Invoke-WebRequest-Uri'http://www.mydomain.com/'-MethodPost-Body$postData-ContentType"text/plain;charset=utf-8" Fiddler: POSThttp://www.mydomain.com/HTTP/1.1 User-Agent:Mozilla/5.0(WindowsNT;WindowsNT6.2;cs-CZ)WindowsPowerShell/4.0 Content-Type:text/plain;charset=utf-8 Host:www.mydomain.com Content-Length:31 zlutouck�kun�peld�belsk��dy Edited: Itseemsthatitisnecessarytofirstconvertthetextintoutf8.PowerShellISEusesadifferentencodingbydefault. Inmycase,windows-1250. $text="žluťoučkýkůňúpělďábelskéódy" $postData=[System.Text.Encoding]::UTF8.GetBytes($text) Invoke-WebRequest-Uri'http://www.mydomain.com/'-MethodPost-Body$postData-ContentType"text/plain;charset=utf-8" powershell-3.0 Share Follow editedFeb10,2014at16:48 Marek askedFeb6,2014at9:13 MarekMarek 23111goldbadge33silverbadges66bronzebadges 3 3 Pleasepostyoureditasananswertoyourquestion;that'showwedothingsaroundhere. – sleblanc Jul11,2014at20:16 [System.Text.Encoding]::UTF8.GetBytes()didn'tworkforme.Outputislike[29,118,61,80,267...] – Groosha Jan1,2016at11:47 PowerShellversionisimportantforthistopic.IcandoUTF8stringaspostbodywithoutanyproblemsonPS5.1butforPS4.0Ineedtosavestringtothefileandthenusewebrequestwith-InFileparameterinsteadof-Body – NikolayKlimchuk May30,2019at16:46 Addacomment  |  2Answers 2 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 23 Thisworkedforme: $postData="žluťoučkýkůňúpělďábelskéódy" Invoke-WebRequest-Uri'http://www.example.com/'-MethodPost-Body$postData -ContentType"text/plain;charset=utf-8" Addingthecharset=utf-8fixedmyissuewheretheacuteaccentcharactersweregettingconvertedtospecialsymbols. Share Follow answeredApr28,2017at16:24 NitinJainNitinJain 27922silverbadges88bronzebadges 1 note:contenttypecanbe"application/json"etc.notrestrictedto"text/plain" – aaron 3hoursago Addacomment  |  8 Ihadtodotwothingstosolvetheproblem.Encodethebodyandaddthecharsettotheheader: $body=[System.Text.Encoding]::UTF8.GetBytes($body); $headers=@{ "Content-Type"="application/json;charset=utf-8"; "OData-MaxVersion"="4.0"; "OData-Version"="4.0"; }; Invoke-WebRequest-Uri"$($odataEndpoint)systemusers($userid)"-MethodPATCH-Headers$headers-Body$body-UseDefaultCredentials Share Follow answeredJul23,2018at14:48 NickNick 73755silverbadges1414bronzebadges 1 IgetbackHeaders:{[Connection,keep-alive],[Keep-Alive,timeout=5],[Content-Length,7],[Content-Type,text/html;charset=utf-8],,myrequestis...-Headers@{'Content-Type'='application/json'}.Ithinkthat-headersor-contenttype`doesnotmatter. – Timo Mar27,2021at13:14 Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedpowershell-3.0oraskyourownquestion. TheOverflowBlog HowtoearnamillionreputationonStackOverflow:beofservicetoothers Therightwaytojobhop(Ep.495) FeaturedonMeta BookmarkshaveevolvedintoSaves Inboximprovements:markingnotificationsasread/unread,andafiltered... Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew... CollectivesUpdate:RecognizedMembers,Articles,andGitLab Shouldweburninatethe[script]tag? Linked 1 Invoke-Webrequest|Invoke-RestMethod-Powershellquestionmarksinsteadofhebrew 0 HowtosendUTF8totheWordPressREST-API? 1 FrenchaccentsinMicrosoftgraph 0 PowershellInvoke-WebrequestencodesfilenameofuploadedfiletoBase64whenitcontainsgermanumlaut 0 Invoke-RestMethodnotacceptingletterswithaccent Related 2 powershellinvoke-webrequestplaintextfile 9 Invoke-WebRequestGetSystemWebProxy() 2 WrongencodinginxmlresponseonPowerShellInvoke-WebRequest 219 Deletefilesolderthan15daysusingPowerShell 77 HideprogressofInvoke-WebRequest 0 PowerShellInvoke-RestMethodMatch 15 Powershell3.0Invoke-WebRequestHTTPSFailsonAllRequests 96 Powershell-WhyisUsingInvoke-WebRequestMuchSlowerThanaBrowserDownload? 1 PowerShellVersion2Invoke-WebRequest HotNetworkQuestions Wouldextractinghydrogenfromthesunlessenitslifespan? WillIgetdeniedentryafterIremovedavisasticker?Ismypassportdamaged? HowradioactivecouldImakesomematerials,whilekeepingthem"cold"? InD&D3.5,canafamiliarbetemporarilydismissed? Whydoesthesameelectrontransitionreleasephotonsofdifferentfrequenciesforsomeelements? Howtoplug2.5mm²strandedwiresintoapushwirewago? Howtoremovetikznode? Interpretinganegativeself-evaluationofahighperformer ElectronicCircuitsforSafeInitiationofPyrotechnics? Isthereawordfor"amessagetomyself"? WhathadEstherdonein"TheBellJar"bySylviaPlath? Howdocucumbershappen?Whatdoes"verypoorlypollinatedcucumber"meanexactly?Howcanpollinationbe"uneven"? circuitikz:Addingarrowheadtotapofvariableinductance? Alternativeversionsofbreathing? Simplebikecomputer Whatisthedefinitionofatrollinthelegalcontext? HowdothosewhoholdtoaliteralinterpretationofthefloodaccountrespondtothecriticismthatNoahbuildingthearkwouldbeunfeasible? Howtosimplifyapurefunction? sshhowtoallowaverylimiteduserwithnohometologinwithpubkey Workplaceidiomfor"beiGelegenheit"-ordertodoeventually,butdonotprovidepriority Adecimal-basedunitoftime Whydostringhashcodeschangeforeachexecutionin.NET? Sciencefictionbook/novelaboutaliensinhumansbodies HowcanIuseWindowstocreateanOSXYosemiteUSBflashdriveinstallerfromthediskimage(.dmg)filedownloadedfromApple? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?