Skip to content

Invoke-RestMethod doesn't return useful info when no data is returned. #5320

Closed

Description

@thezim

Steps to reproduce

You will need to generate an API token and grant the notifications privilege.

PS /Users/greg> $apitoken = "yourapitoken"
PS /Users/greg> $headers = @{ Authorization = "token $apitoken" }
PS /Users/greg> $resp = Invoke-RestMethod -Uri https://api.github.com/notifications -Headers $headers
PS /Users/greg> $resp -eq $null                                                                                                     
PS /Users/greg>

What is returned is an Object.

PS /Users/greg> $resp -is [System.Object]                                                                                           
True
PS /Users/greg> 

Should be no different than this.

PS /Users/greg> $obj = New-Object System.Object                                                                                     
PS /Users/greg> $obj -eq $null                                                                                                      
False
PS /Users/greg> 

Expected behavior

PS /Users/greg> $resp -eq $null
True
PS /Users/greg>

Actual behavior

PS /Users/greg> $resp -eq $null
PS /Users/greg>

Environment data

Name                           Value                                                                                               
----                           -----                                                                                               
PSVersion                      6.0.0-beta.9                                                                                        
PSEdition                      Core                                                                                                
GitCommitId                    v6.0.0-beta.9                                                                                       
OS                             Darwin 17.2.0 Darwin Kernel Version 17.2.0: Fri Sep 29 18:27:05 PDT 2017; root:xnu-4570.20.62~3/R...
Platform                       Unix                                                                                                
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                             
PSRemotingProtocolVersion      2.3                                                                                                 
SerializationVersion           1.1.0.1                                                                                             
WSManStackVersion              3.0

This occurs for Windows PowerShell versions too.

Activity

  1. SteveL-MSFT commented on Nov 3, 2017

    @SteveL-MSFT
    Member

    The repro that you provided doesn't actually work as I get an error Requires authentication. Is what you're expecting is that if the response body is empty, then the output should be $null?

  2. thezim commented on Nov 3, 2017

    @thezim
    ContributorAuthor

    Steve Lee (@SteveL-MSFT) repro updated with the missing piece.

  3. markekraus commented on Nov 3, 2017

    @markekraus
    Contributor

    Greg Zimmerman (@thezim) do you have what the raw response is for that? Is it None or {} or a 0-byte body?

  4. markekraus commented on Nov 3, 2017

    @markekraus
    Contributor

    actually, I was able to do this after clearing all the notifications. it is returning []. for me, this is returning a System.Object[]. which happens to have no elements. I think that is a fair representation of an empty JSON array.

    []
    

    is not the same as

    null
    
  5. thezim commented on Nov 3, 2017

    @thezim
    ContributorAuthor

    Mark Kraus (@markekraus) I agree that it is a fair representation but it is not very intuitive. Most cmdlets follow the null model. Should Get-ChildItem return System.Object[]? The underlying data type (JSON) shouldn't matter since it actually returns a Object.

  6. markekraus commented on Nov 3, 2017

    @markekraus
    Contributor

    The underlying JSON type does matter, actually. If they API says it returns an array that is empty when there are no items, then an empty array is expected. If we return $null instead of an empty array, we break parity with the API. If the API wanted to return $null it would return the JSON equivalent null. This is true of top level and nested items too. the following are equivalent :

    [PSCustomObject]@{
        NestedArray = @()
    } 
    {
      "NestedArray": []
    }

    just as these two are equivalent:

    [PSCustomObject]@{
        NestedNull = $null
    }
    {
      "NestedArray": null
    }

    Even in PowerShell, empty arrays are not null:

    $Obj = [System.Object[]]@()
    $null -eq $Obj

    result: False.

    There is a difference between object serialization and cmdlets which return collections of items. If you used -OutVariable instead of an assignment, you would get an empty ArrayList which isn't null either:

    $null = Get-Process -name 'noloexisto*' -OutVariable Processes
    $Processes.Count
    $Null -eq $Processes

    Result:

    0
    False
    

    In that instance the API is promising to return an ArrayList and does, even though it is empty and it is not evaluated as $null.

  7. markekraus commented on Nov 3, 2017

    @markekraus
    Contributor

    Though, I do see that Invoke-RestMethod is not treating null by itself as $null

    $url = 'http://urlecho.appspot.com/echo?status=200&Content-Type=application%2Fjson&body=null'
    $null -eq (irm $url)

    Result: False

    That should arguably be true. because this does work:

    $url = 'http://urlecho.appspot.com/echo?status=200&Content-Type=application%2Fjson&body=null'
    $null -eq (irm $url |ConvertFrom-Json)

    Result: True

  8. thezim commented on Nov 3, 2017

    @thezim
    ContributorAuthor

    Given deserialization logic irm $url should not be null. If you make it that way you are breaking the API as well.

  9. markekraus commented on Nov 3, 2017

    @markekraus
    Contributor

    Greg Zimmerman (@thezim) null in JSON is serialized as $null in PowerShell. if the API returns nothing but null with the application/json content-type, then irm should return $null, IMO. This works fine when null is nested,

    $url = 'http://urlecho.appspot.com/echo?status=200&Content-Type=application%2Fjson&body=%5Bnull%5D'
    $res =  irm $url
    $res.Count
    $null -eq $res[0]

    Result:

    1
    True
    

    but instead of returning $null when the response is just null, it is returning the string null. A string with null in PowerShell is "null" in JSON, not null.

  10. added
    Issue-Question
    on Nov 3, 2017
  11. SteveL-MSFT commented on Nov 3, 2017

    @SteveL-MSFT
    Member

    My opinion is that an empty array should not be null for the reasons Mark Kraus (@markekraus) provides. As for a single null in the JSON, it seems reasonable for that to be $null and if the string "null" is currently being returned, that looks like a bug.

  12. markekraus commented on Nov 3, 2017

    @markekraus
    Contributor

    Steve Lee (@SteveL-MSFT) it looks like irm handles all the other JSON literals properly

    #String literal
    irm 'http://urlecho.appspot.com/echo?status=200&Content-Type=application%2Fjson&body=%22string%22'
    # False literal
    irm 'http://urlecho.appspot.com/echo?status=200&Content-Type=application%2Fjson&body=false'
    # True lieteral
    irm 'http://urlecho.appspot.com/echo?status=200&Content-Type=application%2Fjson&body=true'
    # number literal
    $res = irm 'http://urlecho.appspot.com/echo?status=200&Content-Type=application%2Fjson&body=5'
    $res.GetType().fullname
    # empty object
    $res = irm 'http://urlecho.appspot.com/echo?status=200&Content-Type=application%2Fjson&body=%7B%7D'
    $res.GetType().fullname

    So it's just null that's not right. Fixing that will be a breaking change, though. I imagine if people were were using an endpoint that returned single values that included null will have logic in place to deal with the broken behavior.

  13. SteveL-MSFT commented on Nov 3, 2017

    @SteveL-MSFT
    Member

    Mark Kraus (@markekraus) is this only the case where there's a single null? if a json object has mutple properties and one or more are null, we deserialize those to $null, correct?

  14. markekraus commented on Nov 4, 2017

    @markekraus
    Contributor

    Steve Lee (@SteveL-MSFT) it is the only case I have found. If it is in an array [null, null, null] it works:

    $res = irm 'http://urlecho.appspot.com/echo?status=200&Content-Type=text%2Fhtml&body=%5Bnull%2Cnull%2Cnull%5D'
    $res.count
    $null -eq $res[0] -and $null -eq $res[1] -and $null -eq $res[2]

    result:

    3
    True
    

    null becomes the string "null" when it is a key in a key/value pair (which we already know null and empty string keys in JSON are problematic), but when its a value in a key/value pair it works fine.

    So the only place it appears to be broken is single value null.

  15. added
    Review - Committee
    on Nov 4, 2017
  16. SteveL-MSFT commented on Nov 4, 2017

    @SteveL-MSFT
    Member

    If we want to take a breaking change, now is the time to do it. I wouldn't do it for 6.1.0.

  17. markekraus commented on Nov 4, 2017

    @markekraus
    Contributor

    #5338 for fixing the null serialization.

  18. added
    Breaking-Change
    Committee-Reviewed
    and removed
    Review - Committee
    on Nov 8, 2017
  19. SteveL-MSFT commented on Nov 8, 2017

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this and agrees that the single null string should be $null value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-ChangeCommittee-ReviewedIssue-QuestionWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions