フィルターのクリア

http HeaderField request invalid

3 ビュー (過去 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 Center および File ExchangeCall 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