500 Error marshalling json from API

I am trying to use the API to change some of the settings on the members of the network, however when I use the post request I just get an error, but it works fine on get requests. This is my code:

import requests
import sys

if __name__ == "__main__":
    api_key = "######"
    network_ID = "#####"
    root = "https://my.zerotier.com/api/v1/"
    auth = {'Authorization': 'Bearer {}'.format(api_key)}

    member_ID = sys.argv[1]

    url = "{}{}/{}/{}/{}".format(root, "network", network_ID, "member", member_ID)

    session = requests.Session()
    session.headers.update(auth)
    response = session.get(url)

    print(response.json())

    payload = {
        "description": "test"
    }

    response = session.post(url, data=payload)
    print(response.status_code)

    print(response.json())

This returns
500
{‘type’: ‘internal’, ‘message’: ‘Error marshalling json’}

Does anyone have any information on how to solve this? Thanks

I’m not that familiar with python these days. It’s been a long time since I have used it. My best guess is that your payload isn’t being serialized to JSON properly, but is rather doing a URL encoded form POST which is not supported by the API.

Doing the same thing via curl on the command line works fine:

curl https://my.zerotier.com/api/v1/network/${NETWORK_ID}/member/${MEMBER_ID} \
-X POST -H 'Authorization: Bearer ${API_KEY}' --data '{"description": "test"}'

Edit:

After reading some docs on the requests library, you likely want to change the post request in your code to this:

response = session.post(url, json=payload)

setting data=... does indeed do a url encoded form post, which is not what you want.

That worked, thank you so much! Not sure if possible, but it would be nice with an error that says something like “Error, url encode not supported” or something like so

It was saying that it couldn’t marshal the JSON posted. It will only do that if it’s not valid JSON, and url encoded form data is not valid JSON data.

Our API only supports JSON, so it was failing as expected.

Yeah, I’m not saying the error was wrong or that it shouldn’t have failed, I’m just saying it was hard to find any information about it when googling the error, so it might be an idea to rethink what the error says to make it easier to troubleshoot.

That being said, I have my background in ML not networking, so it might be a very descriptive and good error for the people that understand the networking language, but for me on the outside, it was not very clear.

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