- 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?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Madheswaran
le 17 Nov 2024 à 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 commentaires
Voir également
Catégories
En savoir plus sur JSON Format dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!