Project

Profile

Help

Feature #6243

open

Extend HTTP client to allow request body objects of other kinds (e.g. File) as accepted by the underlying API

Added by Martynas Jusevicius about 1 year ago. Updated 2 months ago.

Status:
New
Priority:
Normal
Category:
IXSL extensions
Sprint/Milestone:
Start date:
2023-11-07
Due date:
% Done:

0%

Estimated time:
Applies to JS Branch:
2
Fix Committed on JS Branch:
Fixed in JS Release:
SEF Generated with:
Platforms:
Company:
-
Contact person:
-
Additional contact persons:
-

Description

Is there a reason why ixsl:schedule-action/@http-request requires a document node? IIRC it is a wrapper for XMLHttpRequest and it should be possible to send non-XML data.

I'm trying to submit a File but I get this error:

XError:HTTP request body must be a document node, supplied item is: (object) [object File] in HTTP request to: ...

This relates somewhat to media-type and https://saxonica.plan.io/issues/4735#note-1

Actions #1

Updated by Debbie Lockett 4 months ago

Apologies that it has taken so long to respond to this issue.

Could you supply further details of the code that you tried? How exactly did you attempt to supply a File object in your request XDM map? Did you specify the media type for the request content using the media-type entry? If so, what was it?

It looks like the error occurs if the request media type is some XML or HTML type, but the request body is not.

Actions #2

Updated by Martynas Jusevicius 3 months ago

I've created a test for this: https://github.com/namedgraph/saxon-js2-test/blob/gh-pages/client.xsl#L473 I've simply used 'body': $file where the $file was a File object.

Can be tried here by dragging a file onto the "File drop" and observing the console: https://namedgraph.github.io/saxon-js2-test/#file-drop

This time I tried

  • an HTML file with 'media-type': 'text/html' and got the XError:HTTP request body must be a document node error
  • a text file with 'media-type': 'text/plain' and the HTTP request executed as expected (got a different error xsl:message evaluation at client.xsl#464 failed: XError:Internal error: Cannot add item to tree (object) [object XMLHttpRequest]; code:SXJS0004 but not sure if it's related?)
Actions #3

Updated by Debbie Lockett 2 months ago

  • Subject changed from HTTP request body must be a document node to Extend HTTP client to allow request body objects of other kinds (e.g. File) as accepted by the underlying API
  • Category set to IXSL extensions
  • Assignee set to Debbie Lockett
  • Sprint/Milestone set to SaxonJS 3.0

Thanks for the example and additional details, I understand better what you are attempting, and hoping to achieve, now.

Yes, I'm afraid this is currently beyond the capabilities of the SaxonJS HTTP client (i.e. using ixsl:schedule-action/@http-request in SaxonJS 2). As it stands, the body of a request must be an XDM type (e.g. a document node for XML and HTML media types, or a string for text/plain, etc). We do not handle general JavaScript objects - even if they are objects that the underlying JavaScript HTTP API (which SaxonJS uses internally) could handle, e.g. as you say, XMLHttpRequest accepts File, Blob, etc. (and these are also accepted by the Fetch API which will be used in SaxonJS 3).

I'd say this is because rather than being "a wrapper for XMLHttpRequest", the design of the SaxonJS HTTP client was based on the EXPath HTTP Client module, which (understandably) doesn't really consider non-XDM items.

But I agree it would be a good idea to extend the SaxonJS HTTP client API to allow other kinds of JavaScript objects for request and response bodies, as permitted by the underlying JavaScript API. This was not something I had previously considered, because I didn't really understand the use case; but with some small API design and implementation changes it would be possible, and it is worth looking into.

Please register to edit this issue

Also available in: Atom PDF Tracking page