websave - Save content from RESTful web service to file - MATLAB (original) (raw)

Save content from RESTful web service to file

Syntax

Description

outfilename = websave([filename](#buia5%5F1-1-filename),[url](#buia5%5F1-1-url)) saves content from the web service specified by url and writes it to filename. The websave function returns the full filename path asoutfilename.

The web service provides a RESTful interface that returns data formatted as an internet media type, such as JSON, XML, image, or text.

example

outfilename = websave([filename](#buia5%5F1-1-filename),[url](#buia5%5F1-1-url),[QueryName1,QueryValue1,...,QueryNameN,QueryValueN](#buia5%5F1-1-QueryName1QueryValue1QueryNameNQueryValueN)) appends query parameters to url, as specified by one or more pairs of parameter names and values. The web service defines the query parameters.

outfilename = websave(___,[options](#buia5%5F1-1-options)) adds other HTTP request options, specified by the weboptions object options. You can specify this argument in addition to any of the input argument combinations in the previous syntaxes.

websave supports HTTP GET and POST methods. To send an HTTP POST request, specify the RequestMethod property ofoptions as "post". Many web services provide both GET and POST methods to request data.

example

Examples

collapse all

Save an image on a web server to a file in your current folder.

httpsUrl = "https://requestserver.mathworks.com"; imageUrl = strcat(httpsUrl,"/assets/computerVision.jpg"); imageFile = "computerVision.jpg"; imageFileFullPath = websave(imageFile,imageUrl);

Save weather data from a web server file to a CSV file in your current folder.

httpsUrl = "https://requestserver.mathworks.com"; dataUrl = strcat(httpsUrl,"/assets/weatherStation.csv"); weatherFile = "weatherData.csv"; weatherFileFullPath = websave(weatherFile,dataUrl);

Save employee data from a database to a text file in your current folder. Use the fileread and jsondecode functions to extract the JSON-formatted employee data from the text file.

httpUrl = "http://requestserver.mathworks.com"; employeeUrl = strcat(httpUrl,"/employee"); employeeFile = "employeeData.txt"; employeeFileFullPath = websave(employeeFile,employeeUrl,"occupation","Software Engineer"); employeeData = jsondecode(fileread(employeeFileFullPath))

employeeData=2×1 struct array with fields: id firstName lastName occupation age city

Read JSON data from a website and save it in filetest.txt.

uri = matlab.net.URI("http://httpbin.org/get"); websave("test.txt",uri,weboptions("ContentType","json"));

Read the text from the file into a structure of JSON data.

js = jsondecode(fileread("test.txt"))

js =

struct with fields:

   args: [1×1 struct]
headers: [1×1 struct]
 origin: '144.444.4.4'
    url: 'http://httpbin.org/get'

Input Arguments

collapse all

Name of file to save content to, specified as a character vector or string scalar.websave saves the content as is.websave ignoresoptions.ContentType andoptions.ContentReader, even if these properties are set.

Example: websave("matlabcentral.html","https://www.mathworks.com/matlabcentral") reads the specified webpage and saves its HTML to the filematlabcentral.html.

URL to a web service, specified as a character vector or string scalar. Include the transfer protocol. Only http and https are supported. The web service implements a RESTful interface. SeeRESTful for more information.

Web service query parameters, specified as one or more pairs of parameter names and values. AQueryName argument must be a character vector or string scalar that specifies the name of a query parameter. AQueryValue argument must be a character vector, a string scalar, or a numeric, logical, ordatetime value that specifies the value of the query parameter. Numeric, logical, and datetime values can be in arrays. The web service defines the parameters that it accepts as part of a request.

When you specify QueryValue as a datetime value, you must specify its Format property so that it is consistent with the format required by the web service. If theFormat property includes a time zone or offset, and the datetime object is not zoned, thenwebsave specifies "Local" as the time zone.

When QueryValue contains multiple values in an array, you might need to specify the ArrayFormat property of a weboptions object to form-encode the array as required by the web service.

Example: websave("webread_search.html","https://www.mathworks.com/matlabcentral/fileexchange/","term","simulink") retrieves a list of files uploaded to the File Exchange that contain the word simulink and saves the search results to an HTML file.

Additional HTTP request options, specified as a weboptions object. Seeweboptions.

More About

collapse all

REST stands for representational state transfer, a common architectural style for web services. RESTful interfaces provide standard HTTP methods, such as GET, PUT, POST, or DELETE.

Tips

Extended Capabilities

Version History

Introduced in R2014b

expand all

Pressing Ctrl+C while executing this function stops execution of the MATLAB® command. This behavior is consistent with how other MATLAB functionality handles Ctrl+C, as described inUse Keyboard Shortcuts to Navigate MATLAB.

Previously, the function threw a custom exception that users could catch inside a try/catch block. If your code containstry/catch blocks, you can update your code by removing them.

This table shows an example of the different results when you pressCtrl+C before and starting in R2024a.

Before After
try f = websave(myFile,url) % User presses Ctrl+C catch ME rethrow(ME); end K>> ME ME = HTTPException with properties: ... f = websave(myFile,url) % User presses Ctrl+C Operation terminated by user during matlab.internal.webservices.urlencode>validateURL