HTTP400 when making POST query via MATLAB


I’d like to use MATLAB to pull xrd data from the API with a pretty_formula criteria. I can successfully get this data using the command in python as described on the mapidoc github readme. However, I’m unable to replicate this in MATLAB, which is used by everyone in my lab. I am receiving HTTP400 responses no matter what I have tried.

Attempt 1

url = '';
api_key = #########; %my api key goes here

criteria = struct('pretty_formula', 'PbCl2');
data = struct('criteria', criteria, 'properties', 'xrd.Cu');

options = weboptions('KeyName', 'X-API-KEY', 'KeyValue', api_key, 'RequestMethod', 'post', 'MediaType', 'application/json');

r = webwrite(url, data, options)

This returns status 400 “Bad Request”. As far as I can tell, webwrite() does not provide any detailed traceback to diagnose the 400 further.

Attempt 2

url = '';
api_key = ##########; %my api key goes here

headerField ='application/x-www-form-urlencoded');
headerField = addFields(headerField,'X-API-KEY', api_key))

criteria = struct('pretty_formula', 'PbCl2')
data = struct('criteria', criteria, 'properties', 'xrd.Cu')
body =;

request =, headerField, body);
r = send(request, url, options)

which returns an ‘HTTP/1.1 400 Bad Request’ error with the traceback

    'Traceback (most recent call last):
       File "/opt/miniconda3/envs/mpprod3/lib/python3.6/site-packages/django/utils/", line 77, in __getitem__
         list_ = super().__getitem__(key)
     KeyError: 'criteria'
     During handling of the above exception, another exception occurred:
     Traceback (most recent call last):
       File "/var/www/python/matgen_prod/materials_django/rest/", line 94, in wrapped
         d = func(*args, **kwargs)
       File "/var/www/python/matgen_prod/materials_django/rest/", line 204, in query
         criteria = json.loads(post["criteria"])
       File "/opt/miniconda3/envs/mpprod3/lib/python3.6/site-packages/django/utils/", line 79, in __getitem__
         raise MultiValueDictKeyError(key)
     django.utils.datastructures.MultiValueDictKeyError: 'criteria'

Any ideas on how to work through this? If we can figure this out I believe it would be a helpful addition to the documentation, as I imagine many users would be interested in performing more advanced queries from MATLAB.

1 Like

Hi @RishiEKumar,

I did some poking around. The short answer to your question is that you need to treat “criteria” and “properties” as fields whose data are JSON-serialized strings:

url = '';
api_key = '###########';

criteria = struct('pretty_formula', 'PbCl2');
props = 'xrd.Cu';

options = weboptions('KeyName', 'X-API-KEY', 'KeyValue', api_key, ...
'RequestMethod', 'post', 'Debug', true);     % 'Debug': true outputs extra info

r = webwrite(url, 'criteria', jsonencode(criteria), ...
    'properties', jsonencode(props), options)

I had to back out the request from the Python interface by doing:

import requests
import json
data = {
    'criteria': {'pretty_formula': 'PbCl2'},
    'properties': 'xrd.Cu'
response =, headers={'X-API-KEY': api_key},
    data={k: json.dumps(v) for k, v in data.items()})    # This is where the values of the fields are converted to JSON strings
print(response.request.body)    # You can explore the constructed request from the response
# 'criteria=%7B%22pretty_formula%22%3A+%22PbCl2%22%7D&properties=%5B%22xrd.Cu%22%5D'

I will pass along your suggestion to the team to improve the documentation to make this more clear in case other people are using packages other than Python to make the API request.