REST API returns XML, not JSON

I've used the Metabase API before so have a rough idea of what I'm doing, but I'm now trying to use it for a cloud instance instead of one on-prem and I'm having some issues getting JSON back rather than XML

I'm using Python and the requests module -- a selection of the code is below with the sensitive info redacted:

import json
import requests


mb_conn = MetabaseConnector()


class MetabaseConnector(object):
    def __init__(self):
        self.base_url = 'http://mydomain.net/api/'
        self.__api_key = KEY
        self.__api_secret = SECRET
        self.__auth_token = None

        print('Signing in...')
        sign_in_response = self.sign_in()
        print(sign_in_response.text)
        self.__auth_token = json.loads(sign_in_response.text)['id']

    @property
    def auth_token(self) -> str:
        return self.__auth_token

    @property
    def request_headers(self) -> dict:
        if self.auth_token is None:
            return {
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }
        else:
            return {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'X-Metabase-Session': self.auth_token
            }

    def sign_in(self) -> requests.Response:
        """
        https://www.metabase.com/docs/latest/api-documentation.html#post-apisession
        """
        endpoint = 'session'
        body = {
            'username': self.__api_key,
            'password': self.__api_secret
        }
        return requests.request(
            method='POST',
            url=self.base_url + endpoint,
            headers=self.request_headers,
            data=json.dumps(body)
        )

This works on an on-prem instance and prints the {"id": "session-key"} like I expect, but running this on a cloud instance brings back XML data without the id JSON hidden anywhere

Our instance is redirecting traffic to use HTTPS which I think is causing the issue -- when I change http to https for the on-prem API calls I get the same behaviour (XML instead of JSON)

Is there something that I could do to get the API requests to work, or would we have to disable the HTTPS redirects?

EDIT
I'll add that I'm not just getting back the "API data" formatted as XML rather than JSON, I'm getting some webpage XML back instead (the XML I'd get through my web browser)

Hi @bill.wallis
Metabase does not return XML. Then it's something else that is returning data.
The easiest way to learn the API is to just use the regular Metabase interface and open your browser developer console on the Network-tab, so you can follow requests and then click to see the exact parameters and responses.
I would recommend that you post the responses and use cURL verbose debugging to see all redirects.

1 Like

Thanks @flamber, didn't even think about checking the redirect -- turns out it's because of our Microsoft authentication, and the XML (or HTML?) I'm getting back corresponds to the Microsoft sign-in webpage

Definitely not a Metabase issue :slight_smile: