- https://mathworks.com/help/matlab/ref/webwrite.html
- https://mathworks.com/help/matlab/ref/weboptions.html
- https://mathworks.com/help/matlab/ref/jsonencode.html
webwrite returns error 405, how to find its detailed cause?
21 ビュー (過去 30 日間)
古いコメントを表示
I am trying to get certain graphiql working, but am getting 405 error without any detailed error description.
url = 'https://rata.digitraffic.fi/api/v2/graphql/graphiql';
query = '{ currentlyRunningTrains { trainNumber }}';
options = weboptions('KeyName','Accept-Encoding','KeyValue','gzip');
res = webwrite(url, query, options);
Is it possible to find what is the exact cause for this HTTP 405 error?
P.S. is there any way to properly add
Accept-Encoding: gzip
field into weboptions?
0 件のコメント
回答 (1 件)
Madheswaran
2024 年 11 月 17 日 11:14
Hi @Sven Larsen
To access GraphQL data from the server using 'gzip' encoding, please consider the following adjustments to your code:
% Correct the GraphQL endpoint URL
url = 'https://rata.digitraffic.fi/api/v2/graphql/graphql';
% Define the query
query = '{ currentlyRunningTrains { trainNumber }}';
% Set up web options with the necessary headers
options = weboptions;
options.HeaderFields = {'Content-Type', 'application/json'; 'Accept-Encoding', 'gzip'};
% Encode the query in JSON format
queryStruct.query = query;
jsonQuery = jsonencode(queryStruct);
% Send the request and display the response
response = webwrite(url, jsonQuery, options);
disp(response.data);
In this updated version, the URL was corrected to point to the actual GraphQL endpoint instead of the GraphiQL interface. The weboptions were configured to include the necessary headers, specifically 'Content-Type' and 'Accept-Encoding'. Additionally, the query was encoded in JSON format before sending the request.
For more information, refer to the following MathWorks documentation:
Hope this resolves your issue!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で JSON Format についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!