LWM2M add device protocols via script (.xml files)

Product/components used and version/fix level:

cumulocity version: backend: 1016.0.256, UI: 1016.0.256

Detailed explanation of the problem:

i want to upload lwm2m device protocols to a tenant via phyton script but i always get error message.
manually this is done via UI {{tenantURL}}/apps/devicemanagement/index.html#/deviceprotocols => button “add device protocol” (right upper corner) => button: “LWM2M” => select .xml file => finished

via developer tool in browser you can see that this is done with a POST request:
request URL: {{tenanturl}}/service/lwm2m-agent/objectResourceMappings/ddf
payload: file: (binary)
accept: application/json
content-type: multipart/form-data; boundary=----WebKitFormBoundaryjGpksrHDnUpqpSFK

this is my python function where i am trying to do this via script:

url_upload_binary_file = "{{tenant_domain}}/service/lwm2m-agent/objectResourceMappings/ddf".replace("{{tenant_domain}}", tenant_domain)
    print("url upload application = : ", url_upload_binary_file)

    file_name = "1-Server-1_0.xml"
    c8y_headers_newTenant["Content-Type"] = "multipart/form-data; boundary=----WebKitFormBoundaryjGpksrHDnUpqpSFK"
    response = requests.post(url_upload_binary_file, headers=c8y_headers_newTenant, files = files)

Error messages / full error message screenshot / log file:

this is the error file that i receive:

upload application FAILED status code = 500
b’{“message”:“Required request part 'file' is not present”,“error”:“LWM2M/Error”}’

is anybody alrready doing such things and can help here?

Question related to a free trial, or to a production (customer) instance?


1 Like

I have made it work - with following code:

        payload = b"------WebKitFormBoundaryEyllBBRWvySJpES3\r\n"
        payload += 'Content-Disposition: form-data; name="file"; filename="{}"\r\n'.format(file_name).encode("utf-8")
        payload += b"Content-Type: text/xml\r\n\r\n"

        with open(path_to_binary_file, "rb") as file:
            file_data = file.read()

        # add binary data to payload
        payload += file_data

        payload += b"\r\n------WebKitFormBoundaryEyllBBRWvySJpES3--\r\n"

        c8y_headers_newTenant["Content-Type"] = "multipart/form-data; boundary=----WebKitFormBoundaryEyllBBRWvySJpES3"
        response = requests.post(url_upload_binary_file, headers=c8y_headers_newTenant, data=payload)
        payload = response.json()

i will close this post

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.