Add basic Authorization in Matlab script

2 ビュー (過去 30 日間)
Dion Theunissen
Dion Theunissen 2022 年 8 月 12 日
回答済み: Piyush Dubey 2023 年 7 月 3 日
I try to realize a PUT api in matlab. I have allready this part which is working:
s.id = "e37b35dd-4aca-4ecb-8972-55c3a27a8b11";
data = jsonencode(s,PrettyPrint=true)
% data = jsondecode(s,Prettyprint=true);
body = matlab.net.http.MessageBody(data);
% authorizationField = matlab.net.http.field.AuthorizationField(username,password)
contentTypeField = matlab.net.http.field.ContentTypeField('application/json');
header = [contentTypeField]
method = matlab.net.http.RequestMethod.PUT;
uri = "https://apps.reeleezee.nl/api/v1/4a2dfa57-ff9a-400b-9c3a-b6a3beafd597/salesinvoices/fa19e531-ca5f-4682-b62a-f95d80175440"
request = matlab.net.http.RequestMessage(method,header,body);
show(request)
resp = send(request,uri)
But now i need to add mu authorization. This is a basic authorization (username and password). How can i put this in the header from the script above?

回答 (1 件)

Piyush Dubey
Piyush Dubey 2023 年 7 月 3 日
Hi Dion,
AuthorizationFieldclass can be used to add authorization in header of a MATLAB script.
This is a sample code demonstrating its usage:
credentials = matlab.net.http.Credentials.basic(username, password);
authorizationField = matlab.net.http.field.AuthorizationField(credentials);
header = [contentTypeField, authorizationField];
Follow the documentation link below to know more aboutAuthorizationField’ class:
Hope this helps.

カテゴリ

Help Center および File ExchangeCall Web Services from MATLAB Using HTTP についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by