26 #if defined (HAVE_CONFIG_H)
61 DEFUN (urlwrite, args, nargout,
112 int nargin = args.length ();
115 if (nargin != 2 && nargin != 4)
118 std::string url = args(0).xstring_value (
"urlwrite: URL must be a string");
121 std::string filename = args(1).xstring_value (
"urlwrite: LOCALFILE must be a string");
128 method = args(2).xstring_value (
"urlwrite: METHOD must be a string");
130 if (method !=
"get" && method !=
"post")
131 error (R
"(urlwrite: METHOD must be "get" or "post")");
133 param = args(3).xcellstr_value ("urlwrite: parameters (PARAM) for get and post requests must be given as a cell array of strings");
135 if (param.
numel () % 2 == 1)
136 error (
"urlwrite: number of elements in PARAM must be even");
148 if (! ofile.is_open ())
149 error (
"urlwrite: unable to open file");
160 error (
"support for URL transfers was disabled when Octave was built");
166 if (url_xfer.
good ())
171 if (url_xfer.
good ())
177 if (nargout < 2 && ! url_xfer.
good ())
183 DEFUN (urlread, args, nargout,
227 int nargin = args.length ();
230 if (nargin != 1 && nargin != 3)
233 std::string url = args(0).xstring_value (
"urlread: URL must be a string");
240 method = args(1).xstring_value (
"urlread: METHOD must be a string");
242 if (method !=
"get" && method !=
"post")
243 error (R
"(urlread: METHOD must be "get" or "post")");
245 param = args(2).xcellstr_value ("urlread: parameters (PARAM) for get and post requests must be given as a cell array of strings");
247 if (param.
numel () % 2 == 1)
248 error (
"urlread: number of elements in PARAM must be even");
251 std::ostringstream buf;
256 error (
"support for URL transfers was disabled when Octave was built");
269 if (nargout < 2 && ! url_xfer.
good ())
275 DEFUN (__restful_service__, args, nargout,
281 int nargin = args.length ();
286 std::string url = args(0).xstring_value (
"__restful_service__: URL must be a string");
288 std::ostringstream content;
293 error (
"support for URL transfers was disabled when Octave was built");
297 std::string data, method;
310 for (
int i = 0; i < keys.
numel (); i++)
312 if (keys(i) ==
"Timeout")
314 float timeout =
object.get (keys(i)).float_value ();
315 options.
Timeout =
static_cast<long>(timeout * 1000);
318 if (keys(i) ==
"HeaderFields")
320 options.
HeaderFields =
object.get (keys(i)).cellstr_value ();
326 if (keys(i) !=
"Timeout" && keys(i) !=
"HeaderFields"
327 && keys(i) !=
"delete" && keys(i) !=
"display")
329 std::string value =
object.get (keys(i)).string_value ();
331 if (keys(i) ==
"UserAgent")
334 if (keys(i) ==
"Username")
337 if (keys(i) ==
"Password")
340 if (keys(i) ==
"ContentReader")
344 if (keys(i) ==
"RequestMethod")
347 if (keys(i) ==
"ArrayFormat")
350 if (keys(i) ==
"CertificateFilename")
359 if (nargout < 2 && ! url_xfer.
good ())
360 error (
"__restful_service__: %s", url_xfer.
lasterror ().c_str ());
362 return ovl (content.str ());
octave_idx_type numel(void) const
Number of elements in the array.
void add_fcn(void(*fcn)(Params...), Args &&... args)
static std::string make_absolute(const std::string &s, const std::string &dot_path=get_current_directory())
std::string lasterror(void) const
void http_action(const Array< std::string > ¶m, const std::string &action)
bool is_valid(void) const
void set_weboptions(const struct weboptions ¶m)
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,...)
int unlink(const std::string &name)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
octave_value::octave_value(const Array< char > &chm, char type) return retval
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 void delete_file(const std::string &file)