I can not authorize my API call

조회 수: 3 (최근 30일)
Dion Theunissen
Dion Theunissen 2022년 8월 17일
답변: Suraj 2023년 9월 13일
I try to create a PUT API call but I am not able to authorize my call.
username = 'APIDionSimi';
password = '****';
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("APIDionSimi","NOgp!3808")
contentTypeField = matlab.net.http.field.ContentTypeField('application/json');
% autho = matlab.net.http.Credentials("Username","APIDionSimi","Password","NOgp!3808")
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)
I tried several options without succes. Anyone who can help?
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2022년 8월 31일
@Dion Theunissen - are you observing errors? Can you show the expected API format that you need to follow?

댓글을 달려면 로그인하십시오.

답변 (1개)

Suraj
Suraj 2023년 9월 13일
Hi Dion,
I understand that you’re trying to make a "PUT request" but are facing an issue in setting the Authorization headers.
You can do this using “HTTPOptions” class and “Credentials” class in in MATLAB. Here is a code snippet to help you with the same –
import matlab.net.http.*;
% Set the username and password for authorization
username = 'loremipsum';
password = '****';
% Specify the URI (URL) for the API call
uri = "<your_url_here>";
% Specify the HTTP method as PUT
method = RequestMethod.PUT;
% Create the content type field for the header
contentTypeField = field.ContentTypeField('application/json');
header = [contentTypeField];
% Create the credentials for authorization
credentials = Credentials('Username', username, 'Password', password);
% Set the HTTP options and assign the credentials
options = HTTPOptions;
options.Credentials = credentials;
% Create the data to be sent in the request body
s.id = "e37b35dd-4aca-4ecb-8972-55c3a27a8b11";
data = jsonencode(s, 'PrettyPrint', true);
body = MessageBody(data);
% Create the request message with the specified method, header, and body
req = RequestMessage(method, header, body);
% Send the request to the specified URI with the options
resp = send(req, uri, options)
Here are the links to the documentation of some the above-mentioned classes:
  1. "Credentials" class: https://www.mathworks.com/help/matlab/ref/matlab.net.http.credentials-class.html
  2. "HTTPOptions" class: https://www.mathworks.com/help/matlab/ref/matlab.net.http.httpoptions-class.html
I hope this helps!
Thanks & regards,
Suraj.

카테고리

Help CenterFile Exchange에서 Call 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