bugGNU Octave - Bugs: bug #56624, webwrite only supports...

 
 

bug #56624: webwrite only supports form-encoded parameters, not user-defined data

Submitter:  Mike Miller <mtmiller>
Submitted:  Fri 12 Jul 2019 09:01:22 PM UTC
   
 
Category:  Octave Function Severity:  1 - Wish
Priority:  5 - Normal Item Group:  Feature Request
Status:  Confirmed Assigned to:  siko1056
Originator Name:  Open/Closed:  * Open
Release:  * dev Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Wed 20 Apr 2022 10:44:48 AM UTC, comment #3: 

Temporary solution for request with data string (json blob, etc) without query string



(file #53120, file #53121)

Ivan <ivan2707>
Wed 20 Apr 2022 10:15:43 AM UTC, comment #2: 

Usage of Key-Value pairs together with option 'Headerfields' or an option 'Headerfields' with array greater than 1x2
causes error in Octave 6.2.0 webwrite and weboptions functions

webwrite:

  1. key-value pairs processing


## If MediaType is set by the user, append it to other headers.
  if (! strcmp (options.CharacterEncoding, "auto"))
    options.HeaderFields{end+1, 1} = "Content-Type";
    options.HeaderFields{end, 2} = [options.MediaType, ...
                                    "; charset=", options.CharacterEncoding];
  endif

  if (! isempty (options.KeyName))
    options.HeaderFields{end+1, 1} = options.KeyName;
    options.HeaderFields{end, 2} = options.KeyValue;
  endif

00 may be changed to match options class check for field size in weboptions

  ## If MediaType is set by the user, append it to other headers.
  if (! strcmp (options.CharacterEncoding, "auto"))
      options.HeaderFields(end+1, 1:2) = {"Content-Type",...
                                          [options.MediaType, ...
                                           "; charset=", options.CharacterEncoding]};
  endif

  if (! isempty (options.KeyName))
      if iscell(options.KeyName)
          l = length(options.KeyName);
          if (size(options.KeyName,1) == 1)&&(l~=1)
              options.HeaderFields(end+1:end+l, 1:2) = [options.KeyName', options.KeyValue'];
          else
              options.HeaderFields(end+1:end+l, 1:2) = [options.KeyName, options.KeyValue];
          end
      else
          options.HeaderFields(end+1, 1:2) = {options.KeyName, options.KeyValue};
      endif
  endif

  1. headerfields reshape


## Flatten the cell array because the internal processing takes place on
  ## a flattened array.
  options.HeaderFields = options.HeaderFields(:)';

00 may be changed to match cell array format in the backend function '__restful_service__' (not Key ... Key, Value ... Value, but Key, Value, ... Key, Value)

options.HeaderFields = reshape(options.HeaderFields',1,[]);

  1. varargin option


response = __restful_service__ (url, varargin, options);

or

response = __restful_service__ (url, varargin{:}, options);

  1. iscellstr check blocks cell string argument <data>


elseif  (! iscellstr (varargin))
      error ("webwrite: DATA must be a string");

or

elseif  (! iscellstr (varargin{1}))

00 '__restful_service__' requires in  argument <data> a cell string of size 1xN or Nx1 with key-value order


weboptions:

  1. Headerfields check


    function f = set.HeaderFields (f, value)
      ...
        elseif (columns (value) != 2)
          error ("weboptions: HeaderFields must be of size m-by-2");
      ...

00 may be changed to match 1xN reshaped array for '__restful_service__'

    function f = set.HeaderFields (f, value)
      ...
        elseif (columns (value) != 2) && mod(numel(value),2) ~= 0
          error ("weboptions_test: HeaderFields must be of size m-by-2");
      ...


(file #53117, file #53118, file #53119)

Ivan <ivan2707>
Tue 16 Jul 2019 07:00:34 AM UTC, comment #1: 

Confirmed.  Thanks for observing this case Mike.  Unfortunately, this can't be handled by a simple branch:


if (strcmp (options.MediaType, "application/x-www-form-urlencoded"))
  param = strsplit (varargin{1}, {"=", "&"});
...


The "backend function" `__restful_service__` requires a Nx2 cell array of key-value string pairs.  WIP.

Kai Torben Ohlhus <siko1056>
Group Member
Fri 12 Jul 2019 09:01:22 PM UTC, original submission:  

Octave's webwrite function only allows data to be POSTed in form-encoded key-value pairs. It can't yet be used to send user-defined data such as a XML or JSON blob.

This example should ideally work


options = weboptions ('MediaType', 'text/plain')
response = webwrite ('http://localhost/post', 'This is a test message', options)


and should be the same as


curl -X POST -H 'Content-Type: text/plain' -d 'This is a test message' 'http://localhost/post'


But it instead tries to parse the 'data' argument into key-value pairs and POST as a form anyway.

This is related to the message in weboptions that the MediaType keyword is not yet implemented. If MediaType is not set, then webwrite seems to be working correctly. This is also a Matlab compatibility issue, but since this function is brand new it's fair to consider it a feature request.

Mike Miller <mtmiller>
Group Member

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #53120:  test_octave_2.m added by ivan2707 (664B - application/octet-stream)
file #53121:  java_http_request.7z added by ivan2707 (6KiB - application/octet-stream)
file #53117:  weboptions_test.m added by ivan2707 (13KiB - application/octet-stream)
file #53118:  test_octave_1.m added by ivan2707 (893B - application/octet-stream)
file #53119:  webwrite_test.m added by ivan2707 (5KiB - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by ivan2707 (Updated the item)
  • -email is unavailable- added by siko1056 (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 8 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-04-20 ivan2707 Attached File- Added test_octave_2.m, #53120
        Attached File- Added java_http_request.7z, #53121
    2022-04-20 ivan2707 Attached File- Added weboptions_test.m, #53117
        Attached File- Added test_octave_1.m, #53118
        Attached File- Added webwrite_test.m, #53119
    2019-07-16 siko1056 StatusNone Confirmed
        Assigned toNone siko1056
    2019-07-12 mtmiller Carbon-CopyRemoved 80942 -

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code