VMware Cloud Community
Vsure
Enthusiast
Enthusiast

vRA Service API Data serialization error when trying to create a Property Definition

This is my first time trying to create any vRA content using the REST API. I've successfully exported my blueprint, form, property group and property definitions using the API.  But when I try to create a new one using the exported JSON I get the following error:

{"errors":[{"code":10104,"source":null,"message":"Data serialization error.","systemMessage":"Could not read message [acceptableTypes: [application/*+json,
application/json]]","moreInfoUrl":null}]}

Thinking there might be something wrong with my JSON I tried using the sample JSON in the "Try It" section of the API Docs and get the same issue.  Before I open a case I thought I would see if anyone has run into this before.  Search results in Google come up with a couple hits that are not related to using the API.

I am using PowerShell and here is what I think are the relavent snippets:

    $json = get-content C:\DATA\vRA_Exports\MyBluePrint\propertyDefinitions\IASTest.json

    $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; “Authorization” = “Bearer $token”}
    $uri = "https://$vraHost/properties-service/api/propertydefinitions/IASTest"

    $response = Invoke-RestMethod -Method Put -Uri $uri -Headers $headers -Body $json

And here is the JSON I'm testing with from the "Try It" API docs:

{
  "id": "IASTest",
  "label": "My Label",
  "description": "Testing Rest API",
  "dataType": {
    "type": "primitive",
    "typeId": "STRING"
  },
  "isMultiValued": false,
  "permissibleValues": {
    "customAllowed": false
  },
  "displayAdvice": "TEXTBOX",
  "orderIndex": 0,
  "tenantId": "test"
  "version": 1,
}

{"errors":[{"code":10104,"source":null,"message":"Data serialization error.","systemMessage":"Could not read message [acceptableTypes: [application/*+json,

application/json]]","moreInfoUrl":null}]}
Reply
0 Kudos
1 Reply
Vsure
Enthusiast
Enthusiast

I found the answer to my own problem this morning.  Long story short, I had to use Invoke-WebRequest instead of Invoke-RestMethod to get the correct JSON format.

"Data Serialization error" clearly means the JSON could not be successfully parsed.  I download Postman so I could properly test using the API examples as there were intended (see https://github.com/vmware-samples/vra-api-samples-for-postman).  After comparing the property definition as returned from Postman with what I was saving using the PowerShell "Invoke-RestMethod | ConvertTo-Json" I found that the JSON was different. 

Invoke-RestMethod returns a PSCustomObject which then has to be converted to JSON.  The PSCustomObject does not convert back to the original JSON exactly the same (see the examples below).  Invoke-WebRequest works exactly the same way but you have access to the original body through the "content" property.

If you look at the two examples below the Invoke-RestMethod resulted in the mandatory parameters having @{} or hash tables!

Example updated PowerShell function using Invoke-WebRequest instead of Invoke-RestMethod:

 

function getPropertyDefinition($vraHost, $token, $id){
    $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; “Authorization” = “Bearer $token”}
    $uri = "https://$vraHost/properties-service/api/propertydefinitions/$id"
    
    $response = Invoke-WebRequest -Method Get -Uri $uri -Headers $headers
    $response.content
}

 

Results from Postman (and Invoke-WebRequest):

 

{
    "id": "IASTest",
    "label": "Test",
    "description": null,
    "dataType": {
        "type": "primitive",
        "typeId": "STRING"
    },
    "isMultiValued": false,
    "displayAdvice": "TEXTBOX",
    "tenantId": null,
    "orderIndex": null,
    "permissibleValues": null,
    "facets": {
        "mandatory": {
            "type": "constant",
            "value": {
                "type": "boolean",
                "value": false
            }
        },
        "regex": {
            "type": "constant",
            "value": {
                "type": "string"
            }
        },
        "encrypted": {
            "type": "constant",
            "value": {
                "type": "boolean",
                "value": false
            }
        }
    },
    "version": 1
}

 

Results from Invoke-RestMethod (notice the @{type=boolean;value=False})

 

{
    "id":  "IASTest",
    "label":  "Test",
    "description":  null,
    "dataType":  {
                     "type":  "primitive",
                     "typeId":  "STRING"
                 },
    "isMultiValued":  false,
    "displayAdvice":  "TEXTBOX",
    "tenantId":  null,
    "orderIndex":  null,
    "permissibleValues":  null,
    "facets":  {
                   "mandatory":  {
                                     "type":  "constant",
                                     "value":  "@{type=boolean; value=False}"
                                 },
                   "regex":  {
                                 "type":  "constant",
                                 "value":  "@{type=string; value=}"
                             },
                   "encrypted":  {
                                     "type":  "constant",
                                     "value":  "@{type=boolean; value=False}"
                                 }
               },
    "createdDate":  "2019-05-09T12:32:01.425Z",
    "lastUpdated":  "2019-05-09T13:04:04.837Z",
    "version":  4
}

Reply
0 Kudos