Hello,
I have some trouble using the REST API. I use this Python code to call make the REST request:

def rest_call(self, path: str, body = None):
        url = "https://" + self.domain + "/api/v1/" + path
        headers = {"accept" : "application/json",
                   "X-API-Key" : self.key }
        data = json.dumps(body).encode("utf-8") if body else None
        request = Request(url, data, headers)
        logging.debug("Opening URL: %s with body: %s", url, body)
        debug(request.data)
        debug(request.get_method())
        with urlopen(request) as response:
            debug("Received Headers: ", response.getheaders())
            debug(response.status)
            content = response.read().decode("utf-8")
            debug(content)

        return json.loads(content)

which works well for requests that do not have post data. But for deleting an alias, which required a list of IDs as post data, I get this output:

DEBUG:root:Opening URL: https://mail.example.de/api/v1/delete/alias with body: ['20']
mailcow_api.py:19 MailcowAPI.rest_call
    request.data: b'["20"]' (bytes) len=6
mailcow_api.py:20 MailcowAPI.rest_call
    request.get_method(): 'POST' (str) len=4
mailcow_api.py:23 MailcowAPI.rest_call
    response.status: 200 (int)
mailcow_api.py:25 MailcowAPI.rest_call
    content: '{"type":"error","msg":"Cannot find attributes in post data"}' (str) len=60

My two questions are:

  • Why is Mailcow returning error code 200 OK, when there was an error?
  • What is wrong with my request, why doesn’t it sees the post data?

Thanks a lot!

    • Best Answerset by Horus

    Got it fixed, the content-type header was missing:

            headers = {"accept" : "application/json",
                       "X-API-Key" : self.key,
                       'content-type' : 'application/json'}

Got it fixed, the content-type header was missing:

        headers = {"accept" : "application/json",
                   "X-API-Key" : self.key,
                   'content-type' : 'application/json'}

Have something to say?

Join the community by quickly registering to participate in this discussion. We'd like to see you joining our great moo-community!

No one is typing