Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Win8.1 ML7 Roxy rest problem #400

Closed
LynxDelli opened this issue Mar 4, 2015 · 5 comments
Closed

Win8.1 ML7 Roxy rest problem #400

LynxDelli opened this issue Mar 4, 2015 · 5 comments

Comments

@LynxDelli
Copy link

4-3-2015 - 11-38-46
I have ML7 under windows8.1

Installed Roxy app from dev branch with rest.

And to simplify I removed authentication for 4040.

Rest-ext file is:

xquery version "1.0-ml";

module namespace demo = "http://marklogic.com/rest-api/resource/animals";

declare namespace roxy = "http://marklogic.com/roxy";

(:
 :)
declare
%roxy:params("")
function demo:get(
        $context as map:map,
        $params  as map:map
) as document-node()*
{
    map:put($context, "output-types", "application/xml"),
    xdmp:set-response-code(200, "OK"),
    document { "GET called on the ext service extension" }
};


(:
 :)
declare
%roxy:params("animal-type=xs:string", "chip-serial=xs:string", "animal-name=xs:string")
function demo:post(
    $context as map:map,
    $params  as map:map,
    $input   as document-node()*
) as document-node()*
{
  map:put($context, "output-types", "text/html"),
  xdmp:set-response-code(200, "OK"),
  document { <div>PUT called on the ext service extension</div> }
};

Ajax call is:

<!DOCTYPE html>
<html lang="en">
<body>
  <span>test</span>
<script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-1.10.2.min.js"><!-- --></script>
<script>
  $.ajax({
    type: "POST",
    url: '/v1/resources/animals',
    data: {
      'rs:animal-name': 'xxx',
      'rs:animal-type': 'xxx',
      'rs:chip-serial': 'xxx'
    },
    success: function (resp) {
      console.log(resp);
    }
  });
</script>
</body>
</html>

Uploaded and it always call GET instead of POST. When I removed GET have error missing function GET.

What I'm doing wrong?

@grtjn
Copy link
Contributor

grtjn commented Mar 4, 2015

Looking at it..

@grtjn
Copy link
Contributor

grtjn commented Mar 4, 2015

This has more to do with MarkLogic versus AJAX, and not with Roxy, but have an answer for you anyhow..

Took me a moment to figure out, but the AJAX call you are making sends your data as Form Data, and MarkLogic doesn't treat that as a request body. This is mostly caused by the default value for the AJAX contentType setting:

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

add contentType: 'application/json', to your AJAX settings, and it will call the demo:post method instead of the demo:get method..

@grtjn grtjn closed this as completed Mar 4, 2015
@LynxDelli
Copy link
Author

Thanks for the help.
After I added the contentType now it returns Status 200 but no document is
inserted in the DB.
I saw that rs: is important but I guess I miss something else.
I watched - https://www.youtube.com/watch?v=hFBMY6FFp88 and looking at
documentation.

@dmcassel
Copy link
Collaborator

dmcassel commented Mar 4, 2015

Wasn't inserting a document because the call to xdmp:document-insert() was missing from the implementation

@grtjn
Copy link
Contributor

grtjn commented Mar 4, 2015

To elaborate on Dave:

I suspect you want the JSON object you are passing in as data to be stored as document. You don't need to prefix the property names inside the JSON for that. The 'rs:' prefixes only apply to request parameters, but with the current changes you are passing in the JSON object in its entirety. And I think that is what you want as well.

Note though, that the demo:post example function doesn't write anything to the database, it merely replies with some value. You will need to do an xdmp:document-insert('someuri', $input) to actually store what you are sending across..

For a more complete example of how to build an interactive application on top of the REST api, you might be interested looking at https://github.com/marklogic/slush-marklogic-node

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants