Can I send a GET request with body contents in MATLAB R2023b?

11 ビュー (過去 30 日間)
Austin O'Connell
Austin O'Connell 2020 年 10 月 13 日
編集済み: MathWorks Support Team 2023 年 12 月 19 日
Is it possible to send a GET request that includes a body in the "RequestMessage"? I've tried the following code
request = RequestMessage;
request.Body = MessageBody;
request.Body.Data = struct('Name1', 'value1', 'Name2', 'value2');
request.Method = 'get';
uri = URI('http://127.0.0.1/my/api');
[response, completedReq, history] = send(request, uri);
Every time I try this, I get the following response:
Warning: Body unexpected in message using request method GET.
Despite this just being a warning, my web request seems to never even send. Suppressing warnings by doing "warnings('off')" didn't make a difference. Additionally, I have tried using "webread" and "webwrite" instead of "send", but have not had any luck there as well. Any ideas on how to get this working?

採用された回答

Abhisek Pradhan
Abhisek Pradhan 2023 年 11 月 10 日
編集済み: MathWorks Support Team 2023 年 12 月 19 日
In general, the HTTP GET method is used to get data from the server, whereas HTTP PUT and POST methods are used for sending data to the server. MATLAB is strict regarding the rule that GET requests should not contain any body information and the request is modified to a PUT. This change occurred in MATLAB R2019b.
In general, we recommend to avoid sending GET messages with body contents and instead use PUT or POST messages. If you need to send the GET request with a body, the only workaround is to downgrade to MATLAB R2019a or earlier.
  1 件のコメント
Austin O'Connell
Austin O'Connell 2020 年 10 月 19 日
POST and PUT both work as expected.
My confusion was the sentence from the documentation here which states, "Normally, a 'GET' request does not contain data, but the method sends the Body regardless of the RequestMethod." I was under the assumption that we would still be able to send a GET request with a body.
Luckily in my case I have control over the API, so I'll update my API to use a POST instead of GET. Thanks for the help.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by