In MarkLogic 8, if the HTTP call get a response with a recognized json mime type, it will automatically parse it into a document with an object-node. If you receive it as text, you can also use xdmp:unquote, or xdmp:to-json(xdmp:from-json-string(..)) to convert it to the same object-node structure. Note that, though with limitations, you can apply XPath on object-node structure..
Cheers, Geert From: <[email protected]<mailto:[email protected]>> on behalf of Rob Szkutak <[email protected]<mailto:[email protected]>> Reply-To: MarkLogic Developer Discussion <[email protected]<mailto:[email protected]>> Date: Monday, February 1, 2016 at 3:16 PM To: MarkLogic Developer Discussion <[email protected]<mailto:[email protected]>> Subject: Re: [MarkLogic Dev General] HTTP GET Help You've got a few options depending on your preference: #1) Write backend code in JavaScript rather than xQuery #2) Convert the JSON to an XML representation and parse it with xQuery: https://docs.marklogic.com/json:transform-from-json #3) Treat the JSON as an object-node in xQuery: let $obj := object-node { 'foo' : 1, 'bar': 2 } return $obj/foo Best, Rob Rob Szkutak Associate Consultant MarkLogic Corporation [email protected]<mailto:[email protected]> Cell +1.716.562.8464 www.marklogic.com<http://www.marklogic.com> ________________________________ From: [email protected]<mailto:[email protected]> [[email protected]<mailto:[email protected]>] on behalf of Ashley Peacock [[email protected]<mailto:[email protected]>] Sent: Monday, February 01, 2016 8:09 AM To: MarkLogic Developer Discussion Subject: Re: [MarkLogic Dev General] HTTP GET Help Thanks, that seemed to resolve the issue! Another question, if I am expecting JSON back from the HTTP-GET call… what’s the easiest/best way to read that JSON, and then retrieve values from the JSON? For example, let’s say the HTTP call returned: { ‘foo’ : 1, ‘bar’: 2 } I’d want to read the value of foo, for example. Thanks for your help, Ashley [cid:[email protected]] [cid:[email protected]] Ashley Peacock Developer II Elevate Credit International Limited P: 01284 717800 | Ext: 877 | Elevate.co.uk<http://elevate.co.uk> Amadeus House, Floral Street, Covent Garden, London WC2E 9DP Privileged and Confidential. This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain privileged and/or confidential information. If you have received this e-mail in error, please notify me immediately by a return e-mail and delete this e-mail. You are hereby notified that any dissemination, distribution or copying of this e-mail and/or any attachments thereto, is strictly prohibited. Elevate Credit International Limited is registered in England & Wales with Company Number 05041905. Registered Office: 27-28 Eastcastle Street, London, W1W 8DH From: [email protected]<mailto:[email protected]> [mailto:[email protected]] On Behalf Of Rob Szkutak Sent: 01 February 2016 13:11 To: MarkLogic Developer Discussion Subject: Re: [MarkLogic Dev General] HTTP GET Help Hi, I played with this in qconsole and the error goes away if you return a sequence like this: return (xdmp:node-insert-child($doc/root, <insert_timestamp>{fn:current-dateTime()}</insert_timestamp>), xdmp:document-add-collections($cpf:document-uri, ("searchable")), xdmp:log(fn:concat('VARABLE: ', $serverResponse)), cpf:success($cpf:document-uri, $cpf:transition, ())) Alternatively, you can remove the xdmp:log() from your return and instead move it up to your if() .. then and that should work too: if (cpf:check-transition($cpf:document-uri,$cpf:transition)) then try { let $doc := fn:doc($cpf:document-uri) let $serverResponse := xdmp:http-get("my-url") let $_ := xdmp:log(fn:concat('VARABLE: ', $serverResponse)) ... Best, Rob Rob Szkutak Associate Consultant MarkLogic Corporation [email protected]<mailto:[email protected]> Cell +1.716.562.8464 www.marklogic.com<http://www.marklogic.com> ________________________________ From:[email protected]<mailto:[email protected]> [[email protected]<mailto:[email protected]>] on behalf of Ashley Peacock [[email protected]<mailto:[email protected]>] Sent: Monday, February 01, 2016 6:27 AM To: [email protected]<mailto:[email protected]> Subject: [MarkLogic Dev General] HTTP GET Help Hey, I am using the following script in my CPF application, on insert. It worked fine when it was just adding the extra <insert_timestamp /> node, as well as the searchable collection, however I’m trying to make a HTTP GET call and check the response via logging but it throws an error: <error:code>XDMP-UNDVAR</error:code> <error:name>err:XPST0008</error:name> <error:xquery-version>1.0-ml</error:xquery-version> <error:message>Undefined variable</error:message> <error:format-string>XDMP-UNDVAR: (err:XPST0008) Undefined variable $serverResponse</error:format-string> <error:retryable>false</error:retryable> <error:expr> </error:expr> <error:data> My code is: xquery version "1.0-ml"; import module namespace cpf="http://marklogic.com/cpf" at "/MarkLogic/cpf/cpf.xqy"; declare variable $cpf:document-uri as xs:string external; declare variable $cpf:transition as node() external; if (cpf:check-transition($cpf:document-uri,$cpf:transition)) then try { let $doc := fn:doc($cpf:document-uri) let $serverResponse := xdmp:http-get("my-url") (: <options xmlns="xdmp:http"> <authentication method="basic"> <username>myname</username> <password>mypassword</password> </authentication> <data>{$cpf:document-uri}</data> <headers> <content-type>text/plain</content-type> </headers> </options>) :) return xdmp:node-insert-child($doc/root, <insert_timestamp>{fn:current-dateTime()}</insert_timestamp>), xdmp:document-add-collections($cpf:document-uri, ("searchable")), xdmp:log(fn:concat('VARABLE: ', $serverResponse)), cpf:success($cpf:document-uri, $cpf:transition, ()) } catch ($e) { cpf:failure($cpf:document-uri, $cpf:transition, $e, ()) } else () Does anyone know why the this isn’t working? As an FYI, I can make a curl call to the URL fine from the command line and it returns what I expect. If I remove the xmdp:log() call, it also processes fine… I just have no way of seeing the output though. Thanks, Ashley [cid:[email protected]] [cid:[email protected]] Ashley Peacock Developer II Elevate Credit International Limited P: 01284 717800 | Ext: 877 | Elevate.co.uk<http://elevate.co.uk> Amadeus House, Floral Street, Covent Garden, London WC2E 9DP Privileged and Confidential. This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain privileged and/or confidential information. If you have received this e-mail in error, please notify me immediately by a return e-mail and delete this e-mail. You are hereby notified that any dissemination, distribution or copying of this e-mail and/or any attachments thereto, is strictly prohibited. Elevate Credit International Limited is registered in England & Wales with Company Number 05041905. Registered Office: 27-28 Eastcastle Street, London, W1W 8DH Click here<https://www.mailcontrol.com/sr/FyzZ!aw0HQrGX2PQPOmvUmaGI8Tu3yGrSaKuYC6hCX1DI1Zhu4h66oppX6vSA1h!UO+ri9ZlgRzBcfXS4znW5Q==> to report this email as spam.
_______________________________________________ General mailing list [email protected] Manage your subscription at: http://developer.marklogic.com/mailman/listinfo/general
