From: Nick S. <nsi...@nu...> - 2022-10-11 19:11:50
|
Hayim, We use the Expath http-client a lot. Based on the small amount of code that you have pasted below, I can share some code snippets that we might use to do something similar. The below code has been pulled from various files and has not been tested but my hope is that it is close enough to help you identify how to use the http-client library to get the results you are looking for. import module namespace http = "http://expath.org/ns/http-client"; import module namespace jx = "http://joewiz.org/ns/xquery/json-xml" at "../../../core/json-xml/modules/json-xml.xqm"; let $constructed-url := "http://52.12.26.11:8080/collatex/collate" let $headers := <http:header name="Content-Type" value="application/json"/> (: NOTE: headers should be formatted the following way (they do not use a parent tag) <http:header>...</http:header> <http:header>...</http:header> :) let $get-request := <http:request href="{$constructed-url}" method="GET"> <http:header name="Connection" value= "close"/> {$headers} </http:request> let $response := http:send-request($get-request) let $response-body-decoded := util:base64-decode($response[2]) let $parse-response-body := jx:json-to-xml(xs:string($response-body-decoded)) return $parse-response-body Does this help? If you have more questions, let me know. I am happy to help. Nick On 10/11/22 11:52 AM, Hayim Lapin wrote: > Dear list, > This is technically an expath problem rather than an eXist problem, > but I was hoping that the accumulated knowledge on the list might > provide a quick fix and that this is not list abuse. > > I am upgrading an app from eXist version 4 to 5+ and need to rewrite > an http-post request in one function from the older http-client to the > new expath http client. In the attached xql file the post request > begins at line 102. > > My attempted rewrite is wrong. I've tried the following, where $tokens > is a json represntation of textual data from which CollateX > <https://collatex.net> generates an alignment table that is returned > in json as $results > > let $results := parse-json( > content:get-metadata-and-content( > http:send-request(<http:request > href="http://52.12.26.11:8080/collatex/collate" method="post"> > <http:body media-type="text/plain" > content-type="text/plain">{($tokens,$headers)}</http:body> > </http:request> > ))) > return ... (: do stuff with $results :) > > Unfortunately, I cannot figure out how to address the error report > ("Invalid attributes") that eXist returns. > > /db/apps/digitalmishnah/modules/view.xqlerr:XPTY0004 checking > function parameter 1 in call > contentextraction:get-metadata-and-content(untyped-value-check[xs:base64Binary, > hc:send-request(element {"http:request"} { attribute {href} { > http://52.12.26.11:8080/collatex/collate } attribute {method} { > post } text { } element {"http:body"} { attribute {media-type} { > text/plain } attribute {content-type} { text/plain } { ( $tokens, > $headers ) } } text { } } )]): Invalid attributes [at line 106, > column 9] In function: cmp:compare-mishnah(node(), xs:string, > xs:string*, xs:string) > [41:7:/db/apps/digitalmishnah/modules/compare.xql] > cmp:compare-view(node(), map(*)) > [-1:-1:/db/apps/digitalmishnah/modules/compare.xql] > templates:process-output(element(), map(*), item()*, > element(function)) > [211:9:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:call-by-introspection(element(), map(*), map(*), > function(*)) > [189:28:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:call(item(), element(), map(*)) > [137:36:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [148:81:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [148:81:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [148:81:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [148:81:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [148:81:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [148:81:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [148:81:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [428:17:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process-output(element(), map(*), item()*) > [230:9:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process-output(element(), map(*), item()*, > element(function)) > [211:9:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:call-by-introspection(element(), map(*), map(*), > function(*)) > [189:28:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:call(item(), element(), map(*)) > [137:36:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [133:51:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:process(node()*, map(*)) > [90:9:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > templates:apply(node()+, function(*), map(*)?, map(*)?) > [53:5:C:\users\hlapin\eXist-db-6.0.1\data\expathrepo\shared-0.9.1\content\templates.xql] > > > I'd be truly grateful for any pointers. > > Many thanks, > HL > > > > _______________________________________________ > Exist-open mailing list > Exi...@li... > https://lists.sourceforge.net/lists/listinfo/exist-open -- Nick Sincaglia President/Founder NueMeta, LLC Digital Media & Technology Phone: +1-630-303-7035 nsi...@nu... http://www.nuemeta.com Skype: nsincaglia |