webwrite returns error 405, how to find its detailed cause?

21 ビュー (過去 30 日間)
Sven Larsen
Sven Larsen 2022 年 10 月 5 日
回答済み: Madheswaran 2024 年 11 月 17 日 11:14
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?

回答 (1 件)

Madheswaran
Madheswaran 2024 年 11 月 17 日 11:14
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);
currentlyRunningTrains: [114x1 struct]
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:
  1. https://mathworks.com/help/matlab/ref/webwrite.html
  2. https://mathworks.com/help/matlab/ref/weboptions.html
  3. https://mathworks.com/help/matlab/ref/jsonencode.html
Hope this resolves your issue!

カテゴリ

Help Center および File ExchangeJSON Format についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by