Issues with authentication using Restful API

I am using an LED lightsource which is controlled via Restful API. A json encoded string needs to be sent to a specific url address depending on the command. The below line is from the documentation on how to login.
I have achieved this using the following code:
base_url = "http://192.168.7.2:8181/";
command = "api/login";
login.username = "admin";
login.password = "d597cc7b6fca9295";
url = base_url.append(command);
login_json = jsonencode(login);
opt = weboptions('RequestMethod', 'post','MediaType','application/json' ,'Debug', true);
response = webwrite(url, login_json, opt);
The issue I am having is that the documentation says that the retuned object needs to be used as an input argument when calling the other commands.
For example, I want to use the following command:
The following code does not work as it is not passing the login retun object.
command = "api/luminaire/1/command/SET_COLOR";
url = base_url.append(command);
data.arg = [0.5, 0.5, 3400];
data_json = jsonencode(data);
Is there anyway to achieve this with the webwrite and webread functions? Or is there an alternative way? Currently I have it working through python, but I want to get away from calling python code through matlab.
Please note that I have tried including credentials within the weboptions, however as this doesn't allow the url to be included this doesn't do anything.
Any help would be greatly appreciated.
Kind regards,
Robert

2 件のコメント

Geoff Hayes
Geoff Hayes 2022 年 8 月 31 日
@Robert Phipps - you say that the "returned object needs to be used as an input argument when calling the other commands". But from your second example, it isn't clear where the response from the first API request is used. Is there a token that you need to add in your header?
Robert Phipps
Robert Phipps 2022 年 8 月 31 日
@Geoff Hayes I have actually just solved this in the last hour. I had to do it using the HTTP interface. Using the debug option on webwrite I noticed that a cookie is set as per below image. As per the documentation I believed this cookie had to be sent with it, I tested this by coping this into the header of the weboptions which worked.
Unfortunately, I was unable to find a way to retrieve this cookie using webwrite and webread. However, it is possible using the HTTP interface, the below code shows how I solved this.
function Cookie = Login_Lightsource()
import matlab.net.*;
import matlab.net.http.*;
import matlab.net.http.field.*;
uriPost = URI('http://192.168.7.2:8181/api/login');
data = struct ('username', 'admin', 'password', "d597cc7b6fca9295");
acceptHeader = AcceptField('application/json');
contentType = ContentTypeField('application/json');
headers = [contentType, acceptHeader];
httpOpts = HTTPOptions; I had t
r = RequestMessage ('POST', headers, data);
[~, ~, hist] = r.send(uriPost, httpOpts);
cookieInfos = CookieInfo.collectFromLog(hist);
if ~isempty(cookieInfos)
Cookie = [cookieInfos.Cookie];
end
end
function Set_Lightsource_Colour(cookie_data)
import matlab.net.*;
import matlab.net.http.*;
import matlab.net.http.field.*;
uriPost = URI("http://192.168.7.2:8181/api/luminaire/1/command/SET_COLOR");
data = struct ('arg', [0.5, 0.5, 3400]);
acceptHeader = AcceptField('application/json');
contentType = ContentTypeField('application/json');
cookieInfo = CookieField(cookie_data);
headers = [contentType, acceptHeader, cookieInfo];
httpOpts = HTTPOptions;
r = RequestMessage ('POST', headers, data);
[resp] = r.send(uriPost, httpOpts);
out = resp.Body.Data;
end
The login function provides the details for authentication and retrives the cookie, this cookie will then be passed to any following requests.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCall Web Services from MATLAB Using HTTP についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 8 月 16 日

コメント済み:

2022 年 8 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by