How to POST .json file in Powershell without Invoke-WebRequest? -


what doing:

invoke-webrequest -uri https://coolwebsite.com/ext/ext -contenttype application/json -method post -body $somejsonfile 

i looking way post same .json file in powershell without using invoke-webrequest, if possible. new method preferably allow me server output content , parse through in powershell.

maybe calling outside curl method? not sure , internet research has proved fruitless.

how can achieve above result without invoke-webrequest?

you can try :

# restrequest.ps1  add-type -assemblyname system.servicemodel.web, system.runtime.serialization, system.web.extensions  $utf8 = [system.text.encoding]::utf8  function request-rest {   [cmdletbinding()]   param (          [parameter(mandatory=$true)]          [string] $url,           [parameter(mandatory=$false)]          [system.net.networkcredential] $credentials,           [parameter(mandatory=$true)]          [string] $json)    # remove newline json   $json = $json -replace "$([environment]::newline) *",""      # create url instance since httpwebrequest.create method escape url default.      # $url = fix-url $url   $uri = new-object system.uri($url,$true)       try   {     # create request object using uri        $request = [system.net.httpwebrequest]::create($uri)        # build nice user agent        $useragent = "my user agent"     $request.useragent = $("{0} (powershell {1}; .net clr {2}; {3})" -f $useragent, $(if($host.version){$host.version}else{"1.0"}),                              [environment]::version,                              [environment]::osversion.tostring().replace("microsoft windows ", "win"))      $request.credentials = $credentials     $request.keepalive = $true     $request.pipelined = $true     $request.allowautoredirect = $false     $request.method = "post"     $request.contenttype = "application/json"     $request.accept = "application/json"      $utf8bytes = [system.text.encoding]::utf8.getbytes($json)     $request.contentlength = $utf8bytes.length     $poststream = $request.getrequeststream()     $poststream.write($utf8bytes, 0, $utf8bytes.length)     #write-string -stream $poststream -string $json     $poststream.dispose()      try     {       #[system.net.httpwebresponse] $response = [system.net.httpwebresponse] $request.getresponse()       $response = $request.getresponse()     }     catch     {       $response = $error[0].exception.innerexception.response;        throw "exception occurred in $($myinvocation.mycommand): `n$($_.exception.message)"     }      $reader = [io.streamreader] $response.getresponsestream()       $output = $reader.readtoend()        $reader.close()       $response.close()     write-output $output     }   catch   {     $output = @"     {       "error":1,       "error_desc":"error : problème d'accès au serveur $($_.exception.message)"     } "@         write-output $output   } } 

edited 19-10-2015

here example usage :

#$urlbase = "http://192.168.1.1:8080/"   ####################################################################### #                           login                                     # ####################################################################### $wslogin = "production/login" function login {   [cmdletbinding()]   param   (     [validatenotnullorempty()]     [string] $login,     [string] $passwd   )    write-verbose $wslogin    #$jsonin = [pscustomobject]@{"login"=$login;"passwd"=$passwd} | convertto-json   $jsonin = @"   {     "login":"$login",     "passwd":"$passwd"   } "@   write-verbose $jsonin   $jsonout = request-rest -url "$urlbase$wslogin" -json $jsonin -credentials $null   write-verbose $jsonout   #return $jsonout | convertfrom-json    return $jsonout } 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -