HtmlWebResponseObject

Welcome Forums General PowerShell Q&A HtmlWebResponseObject

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
4 weeks, 1 day ago.

  • Author
    Posts
  • #112238

    Participant
    Points: 20
    Published
    Rank: Member

    Hi Guys. Is there any way to view HtmlWebResponseObject output in a screen-readable format?

    I'm building a script and have to work with Rest API and right now just want to be able to view the response in a clean way (on screen).

    It's only for testing purposes, but currently, everything is nested and I have to dig through it each time.

    I'm using this (although really I would like to be selecting * so I can see the response code as well as all the content, but that was just giving me one more step).

    Invoke-WebRequest -Uri $URI -Method POST -Body $postparams | Select content
    
    

    to get this

    
    {"API":{"response":{"operation":{"result":{"message":"Successfully fetched.","created-date":"21/09/2018 08:57 AM","status":"Success","statuscode":200},"Details":{"field-names":{"name...
    
    

    But i'd like to just see all that in one go..

    I tried piping to format-list and its a bit better but still not readable.

    Does anyone have a tip for me? Initially, I thought I could just pipe it to convertto-json and get a plain text output but it's not formatted so that doesn't help

     

    As a workaround right now I am putting each block into a $var and calling it like this $var.content

  • #112241

    Participant
    Points: 20
    Published
    Rank: Member

    damn.. just worked it out after posting this..

    I can use this to get a plain formatted text output

    
    Invoke-WebRequest -Uri $URI -Method POST -Body $postparams | convertfrom-Json | ConvertTo-Json -Depth 20
    
    

    I'd still be curious if there is a better way, but this at least lets me read through it enough to work out what I need.

  • #112252

    Participant
    Points: 19
    Rank: Member

    If it's returning JSON, you should try using Invoke-RestMethod instead; it takes care of most of the conversions automatically.

    • #112253

      Participant
      Points: 20
      Published
      Rank: Member

      Not really sure what the difference is.

      Invoke-RESTMethod -Uri $URI -Method Post -Body $postparams | ConvertTo-Json -Depth 20
      

      Returns the same object, which I convert to JSON to get the same output..

  • #112291

    Participant
    Points: 25
    Rank: Member

    Invoke-RestMethod is returning JSON, so there is no need convert it to JSON again.

    $result = Invoke-RESTMethod -Uri $URI -Method Post -Body $postparams | Select *
    

    That should get you a PSObject. Keep in mind that JSON is typically a nested object, so you'll need to most likely do some parsing work to get exactly what you need, but now that it's a PSObject, you can use simple dot notation to get what you need (e.g. $result.api.response.result)

    • #112489

      Participant
      Points: 20
      Published
      Rank: Member

      Hi Rob. I'm just converting back to JSON so that I can get my format in an easy to read format while I'm testing... When it's in a PSobject, then as you said I have to dot it out but that is tricky when i don't know what i need yet. Converting to JSON with the -depth option allows me to just see plain text on the screen like this.

       

      
      {
      
      "API":  {
      
      "response":  {
      
      "operation":  {
      
      "result":  {
      
      "message":  "Field or column name is not found.",
      
      "status":  "Invalid column(s) specified.",
      
      "statuscode":  3025
      
      },
      
      "name":  "read"
      
      }
      
      },
      
      "version":  1
      
      }
      
      }
      
      

You must be logged in to reply to this topic.