Search code examples
microsoft-graph-apihttp-status-code-415

MS Graph API message move - 415 unsupported media type


I have followed proper documentation and examples. Also other methods are working fine, but message move is not working properly. Please look at below and suggest what I should change:

PS C:\> $body = @{
>>   "destinationId" = "$folderid"   
>> }
## Note: I have tried well known folder types also like deleteditems
PS C:\> $body

Name                           Value
----                           -----
destinationId                  AAMkADIzYjU1MDg4LWIzOTAtNDVhYi1iNjczLTdlNjBiMjExMGE3MwAuAAAAAAAMiK_sOzYGRJ9qF2G24SoEA...


$urimove="https://graph.microsoft.com/v1.0/users/[email protected]/messages/$conv/move"

$body = @{
  "destinationId" = "$folderid"
}

$mv = Invoke-WebRequest -Method Post -Uri $urimove -ContentType "application/x-www-form-urlencoded" -Headers $Headers -Body $body

### if I change above -ContentType "application/x-www-form-urlencoded" to "application/json", I start getting 400 bad request error

ERROR:

Invoke-WebRequest : The remote server returned an error: (415) Unsupported Media Type.
At line:1 char:1
+ Invoke-WebRequest -Method Post -Uri $urimove -ContentType "applicatio ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Solution

  • Finally it worked... below is the solution:

    $urimove="https://graph.microsoft.com/v1.0/users/[email protected]/messages/$Id/move"
    
    $body = @{
      "destinationId" = "$folderid"
    }
    
    $body_json = $body | ConvertTo-Json
    
    $mv = Invoke-WebRequest -Method Post -Uri $urimove -Headers $Headers -Body $body_json
    

    Changes are:

    1. body should be body_json as shown above
    2. no content-type in request call
    3. content-type should be in headers and equals application/json