- 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 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Madheswaran
am 17 Nov. 2024 um 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 Kommentare
Siehe auch
Kategorien
Mehr zu JSON Format finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!