http HeaderField request invalid

9 次查看(过去 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