How do I look at the payload of a webwrite response when the response is a 400 or other error?

조회 수: 7 (최근 30일)
How do I look at the payload of a "webwrite" response when the response is a 400 or other error? It throws a MATLAB error, but doesn't display any of the error messages the server sent back as payload.
My code looks something like this:
json_request = struct('a', nan);
json_response = webwrite('http://localhost:8000/reports/', json_request, ...
weboptions('MediaType', 'application/json', 'RequestMethod', 'post'))

채택된 답변

MathWorks Support Team
MathWorks Support Team 2018년 3월 5일
So basically, you can't get the payload of an error response using "webwrite". However you can get this using the HTTP interface. It might look something like:
import matlab.net.http.*
r = RequestMessage;
resp = r.send(url); %replace url with your target address
show(resp)

추가 답변 (1개)

Richard Zapor
Richard Zapor 2025년 9월 6일 0:14
A slight variation on the Accepted answer is for when Posting a JSON string.
import matlab.net.http.*
body = MessageBody(jsonencode(data)); % or body=data_string of JSON
r = RequestMessage('POST', [], body);
resp = r.send(url);
show(resp)
resp.Body.Data %To see the error message content

카테고리

Help CenterFile Exchange에서 Call Web Services from MATLAB Using HTTP에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by