How to construct complext web URL using matlab.net.http

8 次查看(过去 30 天)
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

回答(1 个)

Hassaan
Hassaan 2024-1-4
% 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);
https://catfact.ninja/fact?as_at_date=2023-10-01T00:00:00&airport_country_name=Israel,Ukraine
% 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);
StatusLine with properties: ProtocolVersion: "HTTP/1.1" StatusCode: OK ReasonPhrase: "OK"
disp(response.Body.Data);
fact: 'The normal body temperature of a cat is between 100.5 ° and 102.5 °F. A cat is sick if its temperature goes below 100 ° or above 103 °F.' length: 136
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

类别

Help CenterFile Exchange 中查找有关 Call Web Services from MATLAB Using HTTP 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by