Main Content

websave

Save content from RESTful web service to file

Description

example

outfilename = websave(filename,url)saves content from the web service specified byurland writes it tofilename. Thewebsavefunction returns the fullfilenamepath asoutfilename.

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

example

outfilename = websave(filename,url,QueryName1,QueryValue1,...,QueryNameN,QueryValueN)appends query parameters tourl, as specified by one or more pairs of name-value arguments. The web service defines the query parameters.

example

outfilename = websave(___,options)adds other HTTP request options, specified by theweboptionsobjectoptions. You can use this syntax with any of the input arguments of the previous syntaxes.

websavesupports HTTP GET and POST methods. To send an HTTP POST request, specify theRequestMethodproperty ofoptionsas的帖子'. Many web services provide both GET and POST methods to request data.

Examples

collapse all

This example shows how to save an image on a Web server to a file.

Locate Image

httpsUrl ="https://requestserver.mathworks.com"; imageUrl = strcat(httpsUrl,“/资产/ computerVision.jpg”);

Save Image to File

imageFile ="computerVision.jpg"; imageFileFullPath = websave(imageFile, imageUrl);

This example shows how to save data from a Web server file.

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

This example shows how to save data to a 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 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.websavesaves the content as is.websaveignoresoptions.ContentTypeandoptions.ContentReader, even if these properties are set.

Example:websave('matlabcentral.html','//www.tatmou.com/matlabcentral')reads the web page 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. Onlyhttpandhttpsare supported. The web service implements a RESTful interface. SeeRESTfulfor more information.

Web service query parameters, specified as one or more pairs of name-value arguments. AQueryNameargument must specify the name of a query parameter. AQueryValueargument must be a character vector, a string scalar, or a numeric, logical, ordatetimevalue that specifies the value of the query parameter. Numeric, logical, anddatetimevalues can be in arrays. The web service defines name-value pairs that it accepts as part of a request.

When you specifyQueryValueas adatetimeobject, you must specify itsFormatproperty to be consistent with the format required by the web service. If theFormatproperty includes a time zone or offset, and thedatetimeobject is not zoned, thenwebsavespecifies'Local'as the time zone.

WhenQueryValuecontains multiple values in an array, you might need to specify theArrayFormatproperty of aweboptionsobject to form-encode the array as specified by the web service.

Example:websave('webread_search.html','//www.tatmou.com/matlabcentral/fileexchange/','term','simulink')retrieves a list of files uploaded to the File Exchange that contain the word金宝appand saves the search results to an HTML file.

Additional HTTP request options, specified as aweboptionsobject. For all request options that areweboptions属性,看到weboptions.

More About

collapse all

RESTful

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

Tips

  • For functionality not supported by the RESTful web services functions, see theUse HTTP with MATLAB.

  • For HTTP POST requests, thewebsavefunction supports only theapplication/x-www-form-urlencodedmedia type. To send a POST request with content of any other internet media type, usewebwrite.

  • To specify proxy server settings, seeProxy Server Authentication.

Version History

Introduced in R2014b