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 requests.post 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 = 'https://www.materialsproject.org/rest/v2/query';
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 = 'https://www.materialsproject.org/rest/v2/query';
api_key = ##########; %my api key goes here
headerField = matlab.net.http.field.ContentTypeField('application/x-www-form-urlencoded');
headerField = addFields(headerField, matlab.net.http.HeaderField('X-API-KEY', api_key))
criteria = struct('pretty_formula', 'PbCl2')
data = struct('criteria', criteria, 'properties', 'xrd.Cu')
body = matlab.net.http.MessageBody(jsonencode(data));
request = matlab.net.http.RequestMessage(method, 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/datastructures.py", 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/rest.py", line 94, in wrapped
d = func(*args, **kwargs)
File "/var/www/python/matgen_prod/materials_django/rest/rest.py", line 204, in query
criteria = json.loads(post["criteria"])
File "/opt/miniconda3/envs/mpprod3/lib/python3.6/site-packages/django/utils/datastructures.py", 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.