Main Content


Save content from RESTful web service to file



outfilename = websave(filename,url) saves content from the web service specified by url and writes it to filename. The websave function returns the full filename path as outfilename.

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

outfilename = websave(filename,url,QueryName1,QueryValue1,...,QueryNameN,QueryValueN) 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) 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 of options as "post". Many web services provide both GET and POST methods to request data.


collapse all

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

httpsUrl = "";
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 = "";
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 = "";
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:

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

uri ="");

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: ''

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 ignores options.ContentType and options.ContentReader, even if these properties are set.

Example: websave("matlabcentral.html","") reads the specified webpage and saves its HTML to the file matlabcentral.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. See RESTful for more information.

Web service query parameters, specified as one or more pairs of parameter names and values. A QueryName argument must be a character vector or string scalar that specifies the name of a query parameter. A QueryValue argument must be a character vector, a string scalar, or a numeric, logical, or datetime 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 the Format property includes a time zone or offset, and the datetime object is not zoned, then websave 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","","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. See weboptions.

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.


  • For functionality not supported by the RESTful web services functions, see Call Web Services from MATLAB Using HTTP.

  • For HTTP POST requests, the websave function supports only the application/x-www-form-urlencoded media type. To send a POST request with content of any other internet media type, use webwrite.

  • For information on how to specify proxy server settings, see Proxy Server Authentication.

Extended Capabilities

Version History

Introduced in R2014b

expand all