MATLAB: MATLAB http post and json request

json http requestMATLAB

Hello
I need please some help to implement the below "Python" code in MATLAB.
I want to be able to perform a request to identify if a key exists and if it is valid through connecting to a certain website
%% Python code ================================
import requests
import json
data = requests.post(
"https://api.somewebsite/accounts/CC/licenses/actions/validate-key",
headers={
"Content-Type": "application/vnd.api+json",
"Accept": "application/vnd.api+json"
},
data=json.dumps({
"meta": {
"key": "xxxxxxxxxxxxxxxxxxx"
}
})
).json()
if data["meta"]["valid"]:
# Do something
else:
# Do something else
%% ================================
many thanks
Mohammad

Best Answer

Hi Mohammad,
I would start with something like this:
import matlab.net.http.*
import matlab.net.http.field.*
request = RequestMessage( 'POST', ...
[ContentTypeField( 'application/vnd.api+json' ), AcceptField('application/vnd.api+json')], ...
'{"meta": {"key": "xxxxxx"}}' );
response = request.send( 'https://api.somewebsite/accounts/CC/licenses/actions/validate-key' );
And use this doc page to work out your logic once you have the response:
For reference, the wider documentation is here:
HTH,
Sylvain