26#if defined (HAVE_CONFIG_H)
57DEFUN (urlwrite, args, nargout,
108 int nargin = args.length ();
111 if (nargin != 2 && nargin != 4)
114 std::string url = args(0).xstring_value (
"urlwrite: URL must be a string");
117 std::string filename = args(1).xstring_value (
"urlwrite: LOCALFILE must be a string");
124 method = args(2).xstring_value (
"urlwrite: METHOD must be a string");
126 if (method !=
"get" && method !=
"post")
127 error (R
"(urlwrite: METHOD must be "get" or "post")");
129 param = args(3).xcellstr_value ("urlwrite: parameters (PARAM) for get and post requests must be given as a cell array of strings");
131 if (param.
numel () % 2 == 1)
132 error (
"urlwrite: number of elements in PARAM must be even");
139 sys::file_stat fs (filename);
142 sys::ofstream (filename.c_str (), std::ios::out | std::ios::binary);
144 if (! ofile.is_open ())
145 error (
"urlwrite: unable to open file");
148 unwind_action_safe unlink_action (unlink_fptr, filename);
150 url_transfer url_xfer (url, ofile);
154 if (! url_xfer.is_valid ())
155 error (
"support for URL transfers was disabled when Octave was built");
157 url_xfer.http_action (param, method);
161 if (url_xfer.good ())
162 unlink_action.discard ();
166 if (url_xfer.good ())
169 retval =
ovl (
"",
false, url_xfer.lasterror ());
172 if (nargout < 2 && ! url_xfer.good ())
173 error (
"urlwrite: %s", url_xfer.lasterror ().c_str ());
178DEFUN (urlread, args, nargout,
222 int nargin = args.length ();
225 if (nargin != 1 && nargin != 3)
228 std::string url = args(0).xstring_value (
"urlread: URL must be a string");
235 method = args(1).xstring_value (
"urlread: METHOD must be a string");
237 if (method !=
"get" && method !=
"post")
238 error (R
"(urlread: METHOD must be "get" or "post")");
240 param = args(2).xcellstr_value ("urlread: parameters (PARAM) for get and post requests must be given as a cell array of strings");
242 if (param.
numel () % 2 == 1)
243 error (
"urlread: number of elements in PARAM must be even");
246 std::ostringstream buf;
248 url_transfer url_xfer = url_transfer (url, buf);
250 if (! url_xfer.is_valid ())
251 error (
"support for URL transfers was disabled when Octave was built");
253 url_xfer.http_action (param, method);
260 retval =
ovl (buf.str (), url_xfer.good (),
261 url_xfer.good () ?
"" : url_xfer.lasterror ());
264 if (nargout < 2 && ! url_xfer.good ())
265 error (
"urlread: %s", url_xfer.lasterror ().c_str ());
270DEFUN (__restful_service__, args, nargout,
276 int nargin = args.length ();
281 std::string url = args(0).xstring_value (
"__restful_service__: URL must be a string");
283 std::ostringstream content;
285 url_transfer url_xfer (url, content);
287 if (! url_xfer.is_valid ())
288 error (
"support for URL transfers was disabled when Octave was built");
292 std::string data, method;
294 struct weboptions options;
297 = args (nargin - 1).classdef_object_value () -> get_object ();
305 for (
int i = 0; i < keys.
numel (); i++)
307 if (keys(i) ==
"Timeout")
309 float timeout =
object.get (keys(i)).float_value ();
310 options.Timeout =
static_cast<long>(timeout * 1000);
313 if (keys(i) ==
"HeaderFields")
315 options.HeaderFields =
object.get (keys(i)).cellstr_value ();
321 if (keys(i) !=
"Timeout" && keys(i) !=
"HeaderFields"
322 && keys(i) !=
"delete" && keys(i) !=
"display")
324 std::string value =
object.get (keys(i)).string_value ();
326 if (keys(i) ==
"UserAgent")
327 options.UserAgent = value;
329 if (keys(i) ==
"Username")
330 options.Username = value;
332 if (keys(i) ==
"Password")
333 options.Password = value;
335 if (keys(i) ==
"ContentReader")
337 options.ContentReader =
"";
339 if (keys(i) ==
"RequestMethod")
342 if (keys(i) ==
"ArrayFormat")
343 options.ArrayFormat = value;
345 if (keys(i) ==
"CertificateFilename")
346 options.CertificateFilename =
"";
350 url_xfer.set_weboptions (options);
352 url_xfer.http_action (param, method);
354 if (nargout < 2 && ! url_xfer.good ())
355 error (
"__restful_service__: %s", url_xfer.lasterror ().c_str ());
357 return ovl (content.str ());
octave_idx_type numel(void) const
Number of elements in the array.
octave_idx_type numel(void) const
OCTINTERP_API void print_usage(void)
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void error(const char *fmt,...)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
int unlink(const std::string &name)
class OCTINTERP_API cdef_object
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
static string_vector make_absolute(const string_vector &sv)