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

21 Ansichten (letzte 30 Tage)
Sven Larsen
Sven Larsen am 5 Okt. 2022
Beantwortet: Madheswaran am 17 Nov. 2024 um 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?

Antworten (1)

Madheswaran
Madheswaran am 17 Nov. 2024 um 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!

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by