RESTful knowledge exchange
Post Reply
jw_ic
Enthusiast
Posts: 34
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Is it possible to send the Request Body as JSON

Post by jw_ic »

Is it possible to send the Request Body as JSON vs XML? I'm using PowerShell so not sure if there is a easy way to convert the parameters needed to xml, but JSON its pretty easy to do.

aka something like:

$data = @{
Overwrite = $False
Username = $domainuser
Password = $password
}
$json = $data | ConvertTo-Json;


Needing to restore file to original location but not overwrite the existing file and it looks like it has to be done in the request body if I read this page correctly:

https://helpcenter.veeam.com/docs/backu ... ml?ver=100
Natalia Lupacheva
Veteran
Posts: 1143
Liked: 302 times
Joined: Apr 27, 2020 12:46 pm
Full Name: Natalia Lupacheva
Contact:

Re: Is it possible to send the Request Body as JSON

Post by Natalia Lupacheva »

Hi James,

Working with Json is possible.
But you have to keep in mind we don't have a lot of examples in json format, so you may have some questions in future (like here).

Thanks!
jw_ic
Enthusiast
Posts: 34
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Is it possible to send the Request Body as JSON

Post by jw_ic »

Anybody know how I can send the request body in with json? or convert it from json to the xml format needed?
jw_ic
Enthusiast
Posts: 34
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Is it possible to send the Request Body as JSON

Post by jw_ic »

When I try and do JSON, been seeing this for trying to restore a file to original location ;

Code: Select all

Invoke-WebRequest : {"FirstChanceExceptionMessage":null,"StackTrace":null,"Message":"The method or operation is not implemented.","StatusCode":500,"Status":"InternalServerError"}
At line:1 char:11
+ $result = Invoke-WebRequest -Uri $uri -method post -Headers $headers  ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Is it possible to send the Request Body as JSON

Post by oleg.feoktistov »

Hi James,

First, regarding the XML vs JSON conversion. As Natalia mentioned, we lack examples in JSON for EM REST, but there are some simple rules, which indicate transformations when re-writing XML to JSON:
  • Root nodes are never reflected, nor written. JSON starts with curly brackets meaning object and child properties written inside:

Code: Select all

{
    "ToOriginalLocation": {
        // properties
    }

}
  • Node attribute always transforms to object property and becomes equal to other properties, which were child nodes in XML:

Code: Select all

{
    "ToOriginalLocation": {
        "Overwrite": "true",
        "GuestCredentials": {
            "UserName": "fileserver01\administrator",
            "Password": "P@ssw0rd"
        }
    }
}
  • As JSON syntax requires, properties, unlike nodes in XML, shouldn't need closing tags.
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Is it possible to send the Request Body as JSON

Post by oleg.feoktistov » 1 person likes this post

Now, about your specific request.
If you want to convert hashtable to json (as your question imply), you need to construct hashtable with child hashtables first. And apply conversion after:

Code: Select all

$creds = @{ UserName = $user; Password = $password }
$spec = @{ Overwrite = $true; GuestCredentials = $creds }
$data = @{ ToOriginalLocation = $spec }
$data = $data | ConvertTo-Json
Then, to send it with Powershell, you would do something like:

Code: Select all

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-RestSvcSessionId", $bearer)
$headers.Add("Accept", "application/json")
$headers.Add("Content-Type", "application/json")
$response = Invoke-RestMethod $url -Method 'POST' -Headers $headers -Body $data
$response | ConvertTo-Json
By the way, I'd advise you to try Postman Tool to visualise what we are discussing here and seamlessly learn to work with APIs. Each request you write there shows code examples for various programming languages. I believe it could help your friendship with REST grow stronger.

Best regards,
Oleg
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Is it possible to send the Request Body as JSON

Post by oleg.feoktistov »

Though the error you shared here doesn't make sense if I got the uri you send the request to correctly.
jw_ic
Enthusiast
Posts: 34
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Is it possible to send the Request Body as JSON

Post by jw_ic »

Ah this may be why I was having issues. I wasn't doing them as child hashtables. This is great information and please add it somewhere to the Rest documentation for json if it isn't alread! Let me try it with these things and see how it goes.
jw_ic
Enthusiast
Posts: 34
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Is it possible to send the Request Body as JSON

Post by jw_ic »

So this definatally got me further and the status of the request says it finished for the file restore. However, I dont see the file restored on the server. I changed the $Overwrite parameter to false so it would restore it as filename.RESTORE but I'm not sure why that file isn't on the server. Is there a place in Enterprise Manager where this job would show up so I can get more details that way? or logs of some sort? I wonder if it had issues logging into the server given the username / password?

Thanks!
jw_ic
Enthusiast
Posts: 34
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Is it possible to send the Request Body as JSON

Post by jw_ic »

So follow up is that I was able to get it working with a non-domain joined server and local account. This server also has file indexing so not sure which issue I ran into above. Going to try and see which was more the cause. Domain joined server, or not having file indexing enabled for the vm.
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Is it possible to send the Request Body as JSON

Post by oleg.feoktistov »

Hi James,

Thanks for the follow-up. Please, share a final outcome as soon as you finish all the testing.

Thanks!
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests