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")");
130 3).xcellstr_value ("urlwrite: parameters (PARAM) for get and post requests must be given as a cell array of strings");
132 if (param.
numel () % 2 == 1)
133 error (
"urlwrite: number of elements in PARAM must be even");
140 sys::file_stat fs (filename);
143 sys::ofstream (filename.c_str (), std::ios::out | std::ios::binary);
145 if (! ofile.is_open ())
146 error (
"urlwrite: unable to open file");
148 int(*unlink_fptr)(
const std::string&) =
sys::unlink;
156 error (
"support for URL transfers was disabled when Octave was built");
162 if (url_xfer.
good ())
167 if (url_xfer.
good ())
173 if (nargout < 2 && ! url_xfer.
good ())
179 DEFUN (urlread, args, nargout,
223 int nargin = args.length ();
226 if (nargin != 1 && nargin != 3)
229 std::string url = args(0).xstring_value (
"urlread: URL must be a string");
236 method = args(1).xstring_value (
"urlread: METHOD must be a string");
238 if (method !=
"get" && method !=
"post")
239 error (R
"(urlread: METHOD must be "get" or "post")");
242 2).xcellstr_value ("urlread: parameters (PARAM) for get and post requests must be given as a cell array of strings");
244 if (param.
numel () % 2 == 1)
245 error (
"urlread: number of elements in PARAM must be even");
248 std::ostringstream buf;
253 error (
"support for URL transfers was disabled when Octave was built");
262 retval =
ovl (buf.str (), url_xfer.
good (),
266 if (nargout < 2 && ! url_xfer.
good ())
272 DEFUN (__restful_service__, args, nargout,
278 int nargin = args.length ();
283 std::string url = args(0).xstring_value (
"__restful_service__: URL must be a string");
285 std::ostringstream content;
290 error (
"support for URL transfers was disabled when Octave was built");
294 std::string data, method;
299 = args (nargin - 1).classdef_object_value () -> get_object ();
307 for (
int i = 0; i < keys.
numel (); i++)
309 if (keys(i) ==
"Timeout")
311 float timeout =
object.get (keys(i)).float_value ();
312 options.
Timeout =
static_cast<long>(timeout * 1000);
315 if (keys(i) ==
"HeaderFields")
317 options.
HeaderFields =
object.get (keys(i)).cellstr_value ();
323 if (keys(i) !=
"Timeout" && keys(i) !=
"HeaderFields"
324 && keys(i) !=
"delete" && keys(i) !=
"display")
326 std::string value =
object.get (keys(i)).string_value ();
328 if (keys(i) ==
"UserAgent")
331 if (keys(i) ==
"Username")
334 if (keys(i) ==
"Password")
337 if (keys(i) ==
"ContentReader")
341 if (keys(i) ==
"RequestMethod")
344 if (keys(i) ==
"ArrayFormat")
347 if (keys(i) ==
"CertificateFilename")
356 if (nargout < 2 && ! url_xfer.
good ())
357 error (
"__restful_service__: %s", url_xfer.
lasterror ().c_str ());
359 return ovl (content.str ());
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
octave_idx_type numel(void) const
void set_weboptions(const struct weboptions ¶m)
bool is_valid(void) const
void http_action(const Array< std::string > ¶m, const std::string &action)
std::string lasterror(void) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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,...)
int unlink(const std::string &name)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
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
static string_vector make_absolute(const string_vector &sv)