Minh Nguyen Posted August 6, 2024 Posted August 6, 2024 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)
Steve Giller Posted August 6, 2024 Posted August 6, 2024 2 minutes ago, Minh Nguyen said: just an HTML code that doesn't say much. Which was?
Minh Nguyen Posted August 6, 2024 Author Posted August 6, 2024 Response code removed to save people from scrolling. It is basically a webpage HTML code
Gareth Cantrell Posted August 6, 2024 Posted August 6, 2024 @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.
Steve Giller Posted August 6, 2024 Posted August 6, 2024 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.
Minh Nguyen Posted August 6, 2024 Author Posted August 6, 2024 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. 1
Minh Nguyen Posted August 6, 2024 Author Posted August 6, 2024 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" } } 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now