How can I debug 400-type error messages from "webread" and "webwrite" in MATLAB R2024b?
10 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2025 年 2 月 11 日
回答済み: Richard Zapor
2025 年 9 月 7 日
I am trying to send a request to a website with the "webwrite" command and capture the output. The website should receive the request and send back a payload of data, but I am getting a vague error that does not help me figure out why I can't access the site.
The error I am seeing is "The server returned the status 403 with message "Forbidden" in response to the request to URL". I can access the site perfectly fine through a browser and outside of MATLAB, so I don't know why I am getting this error when using "webread" in MATLAB.
採用された回答
MathWorks Support Team
2025 年 2 月 11 日
When a website returns a 400 type response to a function like "webread" or "webwrite", there is often some additional data that is sent in the response that is not displayed in the output of the function. This information usually provides a bit of context regarding why the request received that type of response.
To capture the body of the response output, you can use a matlab.net.http.RequestMessage object. You can then inspect the response when sending the request, which will capture the body of the HTTP response instead of showing an error message.
req = matlab.net.http.RequestMessage
req.Method = matlab.net.http.RequestMethod.GET
response = send(req, <url>)
% inspect response.Body.Data
0 件のコメント
その他の回答 (1 件)
Richard Zapor
2025 年 9 月 7 日
A slight variation when sending a JSON string to a site to get a response and catching the error if a 400 event happens
options=weboptions('MediaType', 'application/json');
try
response=webwrite(url,JSON_string,options)
catch
import matlab.net.http.*
body=JSON_string;
r=RequestMessage('POST',[],body);
resp=r.send(url);
show(resp)
resp.Body.Data
end
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!