How to construct complext web URL using matlab.net.http
Show older comments
Hi,
I could could construct this url using matlab.net.http per below. Is there a way to view actual syntax construct sent to http?
apiURL = 'http://lon-wksd-v094:3000/rpc/aviation_grounded';
asAtDate = '2023-10-01T00:00:00';
request= matlab.net.http.RequestMessage;
request.Body = matlab.net.http.MessageBody;
request.Body.Data = struct('as_at_date', asAtDate);
request.Method = 'POST';
uriObj = matlab.net.URI( apiURL);
[ response, completedReq, history] = send( request, uriObj);
How can i construct something like?
Thanks
Answers (1)
% Base URL
apiURL = 'https://catfact.ninja/fact'; % Change it as per your requirements
% Create a URI object from the base URL
uri = matlab.net.URI(apiURL);
% Define the query parameters
asAtDate = '2023-10-01T00:00:00';
airportCountryNames = ["Israel", "Ukraine"]; % Array of country names
% Join the country names with a comma for the query string
countryNamesJoined = strjoin(airportCountryNames, ',');
% Create a QueryParameter array
queryParams = [...
matlab.net.QueryParameter('as_at_date', asAtDate), ...
matlab.net.QueryParameter('airport_country_name', countryNamesJoined)];
% Add the query parameters to the URI
uri.Query = queryParams;
% Convert the URI object to a character vector (string) to get the full URL
urlString = char(uri);
% Display the constructed URL
disp(urlString);
% Prepare the HTTP request
request = matlab.net.http.RequestMessage;
request.Method = matlab.net.http.RequestMethod.GET; % Set the method to GET
% Define the content type as JSON
contentTypeField = matlab.net.http.field.ContentTypeField('application/json');
request.Header = [request.Header; contentTypeField];
% Send the request
uriObj = matlab.net.URI(uri);
response = send(request, uriObj);
% Display the response status line and body
disp(response.StatusLine);
disp(response.Body.Data);
Please ensure that this matches the API specification you're working with. If the server expects a JSON payload in the body of the POST request, you would need to construct a JSON body rather than using query parameters. Adjust the contentTypeField and the request body as necessary based on the server's requirements.
Note:
- I used GET request for demo URL 'apiURL = 'https://catfact.ninja/fact'; % Change it as per your requirements'
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems
- Electrical and Electronics Engineering
Categories
Find more on Call Web Services from MATLAB Using HTTP in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!