Jump to content

Recommended Posts

Posted

Hi all,

 

I am in the phasing of testing something to help with mass updating our assets. There are 2 ways of doing this

1. Using the import tool: I am exploring this but would need an account with the right permission. I am not a Hornbill admin.

2. Using updateAsset2 API . However, I am struggling with getting a test python code to work. The documentation of the format of generalProperties is seriously lacking. I've tried search the forum but there isn't much.

Let's focus on method 2 for now. I've have my code below, run it and didn't get an error in the response, just an HTML code that doesn't say much. For my test, I am just trying to change the description, and the note for an asset which is a Laptop.

 

Could you any give me a hand to see why this is not updating the asset when I check it in the web UI please?

 

import requests
import json

endpoint = "https://api.hornbill.com/[OurInstanceID]/xmlmc/apps/com.hornbill.servicemanager/Asset"
headers = {   
    "Authorization": "API Key created for my account"
}
payload={
        "@service":"apps/com.hornbill.servicemanager/Asset",
        "@method":"updateAsset2",
        "params":{
            "assetId":"215925",
            "assetClass":"computer",
            "generalProperties":[{"name":"description","value":"did this work?"},{"name":"notes","value":"did this work?"}]
            #"additionalProperties":"xs:string",
        },
}
response = requests.request("POST", endpoint, json=payload, headers=headers)
print(response.text)

 

Posted

@Minh Nguyen "generalProperties" (and in fact, all the other properties) is a JSON string, not a JSON object, so it should look like this: 

...
    "generalProperties":"[{\"name\":\"description\",\"value\":\"did this work?\"},{\"name\":\"notes\",\"value\":\"did this work?\"}]"
...

Basically, construct the JSON object and then encode it as a JSON string.

Posted

That returned HTML is a webpage - this implies that your endpoint is not being recognised.

If Gareth's checks don't get it working please double-check your endpoint url and look for any typos in your code.

Posted
3 hours ago, Gareth Cantrell said:

@Minh Nguyen "generalProperties" (and in fact, all the other properties) is a JSON string, not a JSON object, so it should look like this: 

...
    "generalProperties":"[{\"name\":\"description\",\"value\":\"did this work?\"},{\"name\":\"notes\",\"value\":\"did this work?\"}]"
...

Basically, construct the JSON object and then encode it as a JSON string.

 

2 hours ago, Steve Giller said:

That returned HTML is a webpage - this implies that your endpoint is not being recognised.

If Gareth's checks don't get it working please double-check your endpoint url and look for any typos in your code.

 

Thank you both. I think I got this now. For future ref for anyone who may stumble here, there was 2 mistakes I made which @Gareth Cantrell and @Steve Giller pointed out

1. Endpoint address was wrong. My instance ID was correct, but the domain name before that was not the same as the one in the example code (who would have thought). In my case, the domain was lon-p01-api.hornbill.com.  Found this out when using the Dev mode on the browser to snoop around and saw that requests were made to this domain instead of api,hornbill.com

2. The string for generalproperties has to be string(!). Using Gareth's format makes it work. Funny I haven't seen this mentioned in the few posts I found on here.

 

 

  • Like 1
Posted

For future ref, when the endpoint is correct and everything else is correct, response text should be
 

{
        "@status": true,
        "params": {
                "assetId": "LG215925"
        },
        "flowCodeDebugState": {
                "executionId": "a3540059-9b8b-4adf-a6c4-c0e56cc480b7"
        }
}

 

and when the format of the parameter, such as generalProperties, is incorrect, you will see things like this

{
        "@status": false,
        "state": {
                "code": "0203",
                "error": "The property 'methodCall\/params\/generalProperties' is provided as an array but should be a single value"
        }
}

 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...