主要内容

Webwrite

Write data to RESTful web service

描述

example

response= webwrite(URL,Postname1,PostValue1,...,Postnamen,PostValuen)将内容写入指定的Web服务URL并返回response。输入参数Postname1,PostValue1,...,Postnamen,PostValuenspecify the content as name-value pairs.Webwrite形式编码HTTP POST请求的主体中的名称值对。Web服务定义response

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

example

response= webwrite(URL,数据)帖子数据到指定的Web服务URL并设置媒体类型的基础上数据

The input argument数据将内容指定为形式编码的字符数组。Webwrite推杆数据在HTTP POST请求的正文中。Web服务定义response

example

response= webwrite(___,选项)添加其他HTTP请求选项,由weboptionsobject选项。您可以将此语法与先前语法的任何输入参数一起使用。

将内容写为互联网媒体类型以外的其他编码字符阵列('应用程序/x-www-form-urlenCoded'),指定MediaType财产的选项

To request data with an HTTP POST request and read the response with a function, specify theContentReader财产的选项作为功​​能的手柄。如果指定返回多个输出参数的函数的句柄,Webwritereturns all output arguments.

例子

全部收缩

Write a number to a channel feed on the ThingSpeak server and read it back.

要运行此代码,请创建一个Thingspeak帐户。称呼Webwrite使用从您的ThingsPeak帐户中的Write API键和通道ID。The default field name is'field1'

thingSpeakURL = 'http://api.thingspeak.com/'; thingSpeakWriteURL = [thingSpeakURL 'update']; writeApiKey = 'Your Write API Key';fieldName ='field1';fieldValue = 42;response = webwrite(thingSpeakWriteURL,'api_key',writeApiKey,fieldName,fieldValue)

If this call toWebwrite是the first update to your ThingSpeak channel,response1

Read back the number you wrote to your channel. ThingSpeak provides a different URL to get the last entry to your channel. Your Channel ID is part of the URL.

channelID = num2str(Your Channel ID);thingSpeakReadURL = [thingSpeakURL 'channels/' channelID '/fields/' fieldName '/last']; data = webread(thingSpeakReadURL,'api_key',writeApiKey)
数据= 42

This example shows how to write data to a Web server.

httpUrl ='http://requestserver.mathworks.com';DELIM ='&';pairDelim ='=';数据= 42;数据= num2str(data); data = ['key',配对,'value', delim,'field',配对,数据]; responseData = webwrite(httpUrl, data); disp(responseData);
datatype:'application/x-www-form-urlencoded'dataSize:'18'

此示例显示了如何将记录写为JSON对象。

httpsurl ='https://requestserver.mathworks.com';员工(1).name ='Jon';employee(1).Occupation ='医生';员工(2).name ='莎拉';employee(2).Occupation ='工程师';选项= WebOptions('MediaType','application/json');ResponseMployee = WebWrite(httpsurl,员工,选项)
ResponseMployee =带有字段的结构:数据类型:'application/json;charset = utf-8'datasize:'79'

将数字和特定日期写入ThessPeak Server上的频道供稿。阅读编号和日期。

要运行此代码,请创建一个Thingspeak帐户。称呼Webwrite使用从您的ThingsPeak帐户中的Write API键和通道ID。用一个指定提要条目的日期约会时间对象。

thingSpeakURL = 'http://api.thingspeak.com/'; thingSpeakWriteURL = [thingSpeakURL 'update']; writeApiKey = 'Your Write API Key';fieldName ='field1';fieldValue = 42;d = dateTime(2015,3,22,8,15,30,'格式','yyyy-mm-dd HH:MM:SS');响应= webwrite(thingspeakwriteurl,'api_key',writeapikey,... fieldname,fieldvalue,'create_at',d)

If this call toWebwrite是the first update to your ThingSpeak channel,response1

Read back the last entry to your channel. ThingSpeak provides a different URL to get the last entry to your channel. Appendlast.jsonto the URL to get the data as a JSON object. Your Channel ID is part of the URL.

channelID = num2str(Your Channel ID);thingspeakReadUrl = [thesspeakurl'Channels/'ChannelId'/fields/'... fieldName'/last.json'];data = webread(thingspeakreadurl,'api_key',writeapikey)
data = create_at:'2015-03-22T08:15:30z'entry_id:1 field1:'42'

The date in the创建了field matches the date specified inD

Write two name-value pair arguments tohttpbin.org。The site returns the request's POST parameters.

uri = matlab.net.uri('http://httpbin.org/post');res = webwrite(uri,'field1','hello','field2','世界');res.form
ans = struct with fields: field1: 'hello ' field2: 'world'

输入参数

全部收缩

URL to a web service, specified as a character vector or string scalar. Include the transfer protocol. Onlyhttphttpsare supported. The web service implements a RESTful interface. See安息了解更多信息。

Web服务发布参数,指定为一对或多对名称值参数。一个邮政名称argument must specify the name of a post parameter. A后价argument must be a character vector, a string scalar, or a numeric, logical, or约会时间value that specifies the value of the post parameter. Numeric, logical, and约会时间values can be in arrays. The web service defines name-value pairs that it accepts as part of a request.Webwriteencodes the name-value pairs as a form-encoded character array in the body of an HTTP POST request and sets the content type to应用/X-WWW-Form-urlenCodedby default.

When you specify后价as a约会时间object, you must specify its格式属性使其与Web服务所需的格式一致。如果是格式属性包括时区或偏移,约会时间object is not zoned, thenWebwrite指定'Local'as the time zone.

当一个后价参数在数组中包含多个值,指定arrayformat财产的aweboptionsobject to form-encode the array as specified by the web service.

例子:Webwrite('//www.tatmou.com/matlabcentral/fileexchange/','term','webwrite','duration',7)retrieves a list of files uploaded to the File Exchange within the past 7 days that contain the wordWebwrite。文件交换Web服务定义了term期间parameters.

Data to post to a web service, specified as a character vector, a string scalar, or as numeric, cell, logical, or structure forMediaTypevalue'json', or as Document Object Model forMediaTypevalue'XML'。If数据是a character string or character vector, thenWebwritesends it without conversion. All other types are converted based on theWebOptions.mediatype价值。有关更多信息,请参阅https://tools.ietf.org/html/rfc6838

例子:Webwrite('//www.tatmou.com/matlabcentral/fileexchange/','term=webwrite&duration=7')retrieves a list of files uploaded to the File Exchange within the past 7 days that contain the wordWebwrite。文件交换Web服务定义了term期间parameters.

其他HTTP请求选项,指定为weboptions对象。Seeweboptionsfor all request options that areweboptions特性。

Output Arguments

全部收缩

Web服务的响应,作为标量,数组,结构或表返回。

More About

全部收缩

安息

REST方法代表性国家转移,一种用于Web服务的常见建筑风格。RESTFUL接口提供标准的HTTP方法,例如GET,PUT,POST或DELETE。

提示

  • TheWebwritefunction writes帖子,后值输入参数作为形式编码的字符阵列。如果您还指定选项输入参数,然后MediaTypeproperty must be'应用程序/x-www-form-urlenCoded'

  • Webwrite无法转换约会时间objects to JSON, because JSON does not define a standard date format.

  • Webwritealways puts帖子,后值query parameters into the body of the message regardless of the value of theRequestMethod财产的选项

  • 要指定代理服务器设置,请参阅Proxy Server Authentication

Version History

Introduced in R2015a