26 #if defined (HAVE_CONFIG_H)
57 DEFUN (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");
140 sys::ofstream (filename.c_str (), std::ios::out | std::ios::binary);
142 if (! ofile.is_open ())
143 error (
"urlwrite: unable to open file");
145 int(*unlink_fptr)(
const std::string&) =
sys::unlink;
153 error (
"support for URL transfers was disabled when Octave was built");
159 if (url_xfer.
good ())
164 if (url_xfer.
good ())
165 retval =
ovl (sys::env::make_absolute (filename),
true,
"");
170 if (nargout < 2 && ! url_xfer.
good ())
176 DEFUN (urlread, args, nargout,
220 int nargin = args.length ();
223 if (nargin != 1 && nargin != 3)
226 std::string url = args(0).xstring_value (
"urlread: URL must be a string");
233 method = args(1).xstring_value (
"urlread: METHOD must be a string");
235 if (method !=
"get" && method !=
"post")
236 error (R
"(urlread: METHOD must be "get" or "post")");
238 param = args(2).xcellstr_value ("urlread: parameters (PARAM) for get and post requests must be given as a cell array of strings");
240 if (param.
numel () % 2 == 1)
241 error (
"urlread: number of elements in PARAM must be even");
244 std::ostringstream buf;
249 error (
"support for URL transfers was disabled when Octave was built");
253 if (nargout < 2 && ! url_xfer.
good ())
258 retval(0) = buf.str ();
260 retval(1) = url_xfer.
good ();
267 DEFUN (__restful_service__, args, nargout,
273 int nargin = args.length ();
278 std::string url = args(0).xstring_value (
"__restful_service__: URL must be a string");
280 std::ostringstream content;
285 error (
"support for URL transfers was disabled when Octave was built");
289 std::string data, method;
294 = args (nargin - 1).classdef_object_value () -> get_object ();
302 for (
int i = 0; i < keys.
numel (); i++)
304 if (keys(i) ==
"Timeout")
306 float timeout =
object.get (keys(i)).float_value ();
307 options.
Timeout =
static_cast<long> (timeout * 1000);
310 if (keys(i) ==
"HeaderFields")
312 options.
HeaderFields =
object.get (keys(i)).cellstr_value ();
318 if (keys(i) !=
"Timeout" && keys(i) !=
"HeaderFields"
319 && keys(i) !=
"delete" && keys(i) !=
"display")
321 std::string value =
object.get (keys(i)).string_value ();
323 if (keys(i) ==
"UserAgent")
326 if (keys(i) ==
"Username")
329 if (keys(i) ==
"Password")
332 if (keys(i) ==
"ContentReader")
336 if (keys(i) ==
"RequestMethod")
339 if (keys(i) ==
"ArrayFormat")
342 if (keys(i) ==
"CertificateFilename")
351 if (nargout < 2 && ! url_xfer.
good ())
352 error (
"__restful_service__: %s", url_xfer.
lasterror ().c_str ());
354 return ovl (content.str ());
357 OCTAVE_END_NAMESPACE(
octave)
charNDArray max(char d, const charNDArray &m)
charNDArray min(char d, const charNDArray &m)
octave_idx_type numel() const
Number of elements in the array.
octave_idx_type numel() const
void set_weboptions(const struct weboptions ¶m)
std::string lasterror() const
void http_action(const Array< std::string > ¶m, const std::string &action)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#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)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
std::string ContentReader
Array< std::string > HeaderFields
std::string CertificateFilename