Can I use webwrite to make a PUT request?

8 ビュー (過去 30 日間)
Prabhakar Vallury
Prabhakar Vallury 2024 年 9 月 16 日
コメント済み: Prabhakar Vallury 2024 年 9 月 17 日
I've been able to successfully use the 'webwrite' and 'webread' functions for POST and GET requests for REST API calls in Matlab. Now, I need to make a small PUT request without any body/data.
Is there a way to use webwrite for this purpose? I've read about the matlab.net.HTTP.Request Class in THIS page, but it seems complicated - how do I send a BEARER authorization token and the PUT request without any body/data? It would be nice if webwrite could handle PUT requests as well.
Thanks,
PV

採用された回答

Animesh
Animesh 2024 年 9 月 16 日
You can use "weboptions" to specify the request method, such as PUT. You can then pass these web options to "webwrite" to obtain the desired results. Additionally, the bearer authorization token can be included in the web options. Here is a sample code snippet:
url = 'https://demo.url.com';
% This data can be empty if you don't want to pass anything
data = struct('param1', 'value1', 'param2', 'value2');
bearerToken = 'your_bearer_token_here';
options = weboptions('RequestMethod', 'put', ...
'MediaType', 'application/json', ...
'HeaderFields', {'Authorization', ['Bearer ' bearerToken]});
response = webwrite(url, data, options);
You can refer the following documentation for more information:
  1 件のコメント
Prabhakar Vallury
Prabhakar Vallury 2024 年 9 月 17 日
This is great - the documentation from webwrite implies it is only for POST. I would recommend updating it with examples for PUT, DELETE, etc. requests also. Thanks for the help!

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

その他の回答 (1 件)

Gaurav
Gaurav 2024 年 9 月 16 日
You can use the weboptions function to set the request method, like PUT or GET. After setting the desired options, you can pass them to the webwrite function to perform the request. If needed, you can also include a bearer authorization token in the weboptions. Here’s an example pseudocode:
url = 'https://demo.url.com';
% Data can be omitted if not needed
data = struct('param1', 'value1', 'param2', 'value2');
bearerToken = 'your_bearer_token_here';
options = weboptions('RequestMethod', 'put', ...
'MediaType', 'application/json', ...
'HeaderFields', {'Authorization', ['Bearer ' bearerToken]});
response = webwrite(url, data, options);
You can refer the following documentation for more information:
Hope it helps!

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by