MATLAB http post and json request

100 vues (au cours des 30 derniers jours)
mohammad ibrahim
mohammad ibrahim le 8 Jan 2020
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

Réponse acceptée

Sylvain Lacaze
Sylvain Lacaze le 8 Jan 2020
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
  1 commentaire
mohammad ibrahim
mohammad ibrahim le 8 Jan 2020
many thanks Sylvain

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Call Web Services from MATLAB Using HTTP dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by