weboptions

options for REST API web service.

Syntax

options = weboptions()

options = weboptions([Name, value])

Inputs

Name, value
Name
Type: string
Valid options are:
Name
Value
HeaderFields
Specify header for the connection. Header name and value are specified as struct.
Type: struct
Username
username for the connection authentication.
Type: string
Password
password for the connection authentication.
Type: string
RequestMethod
type of request to be made to the connection. valid options are 'get','post','put','delete'. webread uses get as default and webwrite uses post as default.
Type: string
Timeout
timeout value for the connection in seconds. default is 10 seconds.
Type: double

Outputs

options
A struct containing the options.
Type: web options

Examples

set web options with username and password
  options = weboptions('Username','testuser','Password','testpassword','RequestMethod','get','HeaderFields',struct('Content-Type','application/json'));
  res = webread('https://httpbin.org/get',options)
res = {
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Authorization": "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "X-Amzn-Trace-Id": "Root=1-656f998d-4779465e04ec600e1bce5215"
  }, 
  "origin": "68.61.253.8", 
  "url": "https://httpbin.org/get"
}
set web options with for requestmethod put, delete, patch methods
%post method
headers = struct('Content-Type','application/json');
payload = jsonencode(struct('name','Apple AirPods', 'data',struct('color','white', 'generation','3rd', 'price',135)));
url = 'https://api.restful-api.dev/objects'
opts = weboptions('HeaderFields',headers);
out = webwrite(url,payload,opts)

post_out = jsondecode(out);
%put method
opts = weboptions('HeaderFields',headers,'RequestMethod','put');
payload = jsonencode(struct('name','Apple AirPods', 'data',struct('color','white', 'generation','3rd', 'price',100)));
out_put = webwrite([url,'/', post_out.id],payload,opts)

%patch method to update partial data of existing resource
opts_patch = weboptions('HeaderFields',headers,'RequestMethod','patch');
payload_patch = jsonencode(struct('name','Apple AirPods sample patch'));
out_patch = webwrite([url,post_out.id],payload_patch,opts_patch)

%delete method to delete an existing resource
opts_del = weboptions('HeaderFields',headers,'RequestMethod','delete');
out_del = webwrite([url,'/',post_out.id],opts_del)
out = {"id":"ff8081818c01d7ae018c3bd9cee038bd","name":"Apple AirPods","createdAt":"2023-12-05T21:19:39.232+00:00","data":{"color":"white","generation":"3rd","price":135}}
		out_put = {"id":"ff8081818c01d7ae018c481d064148ff","name":"Apple AirPods","updatedAt":"2023-12-08T06:30:56.927+00:00","data":{"color":"white","generation":"3rd","price":100}}
         out_patch = {"id":"ff8081818c01d7ae018c481d064148ff","name":"Apple AirPods sample patch","updatedAt":"2023-12-08T06:28:31.205+00:00","data":{"color":"white","generation":"3rd","price":135}}
		 out_del = {"message":"Object with id = ff8081818c01d7ae018c3bd9cee038bd has been deleted."}