http HeaderField request invalid

조회 수: 11 (최근 30일)
Victor
Victor 2022년 2월 27일
답변: Suraj 2023년 9월 14일
According to provided API, the request must include the following header:
x-http-request-info:{"clientRequestId":{"sessionId":"some_ID_number","requestId":"123456789"}}
If I use the webread, I can construct this as
requestId = struct('clientRequestId',struct('sessionId',sessionId,'requestId',requestId));
header = {'x-http-request-info' jsonencode(requestId)};
options = weboptions('RequestMethod','GET','HeaderFields',header);
response = webread(url, options);
The problem with this approach is that webread does not return response-header, so I have to use the low level http commands.
However matlab.net.http.RequestMessage does not accept the above request header. I have tried to use matlab.net.http.HeaderField:
field1 = matlab.net.http.HeaderField('sessionId', sessionId);
field2 = matlab.net.http.HeaderField('requestId', requestId);
requestInfo = matlab.net.http.HeaderField('clientRequestId',[field1, field2]);
matlab.net.http.HeaderField('x-http-request-info', requestInfo);
method = matlab.net.http.RequestMethod.GET;
request = matlab.net.http.RequestMessage(method,header);
response = send(request,uri);
The response comes back with code 400 BadRequest
response =
ResponseMessage with properties:
StatusLine: 'HTTP/1.1 400 '
StatusCode: BadRequest
Header: [1×8 matlab.net.http.HeaderField]
Body: [1×1 matlab.net.http.MessageBody]
Completed: 0
response.Body.Data.messages
ans =
struct with fields:
key: 'request.header.invalid.x-http-request-info'
severity: 'ERROR'
message: 'Der Header 'x-http-request-info' ist fehlerhaft.'
What is the correct way to specify this particular header?
Many thanks in advance!
Victor

답변 (1개)

Suraj
Suraj 2023년 9월 14일
Hi Victor,
I see that you want to set a custom header for your GET request. “webread” method lets you do this however you mentioned that you want to use a lower-level HTTP method for the same.
I suggest that you checkout “urlread2” from the File Exchange: https://www.mathworks.com/matlabcentral/fileexchange/35693-urlread2
Thanks & regards,
Suraj.

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by