Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ending URL endpoints with '/' when request doesn't have an id #60

Open
lghamie opened this issue Jul 21, 2015 · 5 comments
Open

Ending URL endpoints with '/' when request doesn't have an id #60

lghamie opened this issue Jul 21, 2015 · 5 comments

Comments

@lghamie
Copy link

lghamie commented Jul 21, 2015

Playing with some API i'm triyng to integrate I get some errors that are related to not having a backslash at the end of the URL when POSTing a new object, (without an id).

Reading here at wikipedia I found that this is no standard, but probably looks as the most accepted way of doing this.

So, specifically this function,

function createEndpoint(req, method, config, conn){
  if(_.isObject(conn.options) && conn.options.hasOwnProperty('id')){
    config.endpoint = url.resolve(conn.connection.endpoint + '/', conn.collection + '/' + conn.options.id);
    delete conn.options.id;
  } else {
    config.endpoint = url.resolve(conn.connection.endpoint + '/', conn.collection');
  }
}

I think it should be something like this:

function createEndpoint(req, method, config, conn){
  if(_.isObject(conn.options) && conn.options.hasOwnProperty('id')){
    config.endpoint = url.resolve(conn.connection.endpoint + '/', conn.collection + '/' + conn.options.id);
    delete conn.options.id;
  } else {
    config.endpoint = url.resolve(conn.connection.endpoint + '/', conn.collection' + '/');
  }
}

Note that last forward slash added.

If this is not the way I should do this, because i misunderstood something, please could you oint me in the right direction?

Thanks!

@zohararad
Copy link
Owner

I actually have no idea about the standard, and assume that some implementations ignore it or support both formats (with / without trailing backslash).

Did you try the v0.10 branch? It uses Superagent under the hood to make the HTTP calls, and has pre/post request hooks that allow you to modify things (incl. the end-point).

I would go with either v0.10 (which will soon become the official version), or try to modify the API server to accept both.

@lghamie
Copy link
Author

lghamie commented Jul 22, 2015

Yeah, I think implementations may support different formats.

I tried it using the v0.10 branch, with a before hook to this function

var addFinalSlash = function(params) {
  if (params.url.substr(-1) != '/') param.url += '/';
  return params;
};

But this still sends to the endpoint without the trailing backslash.

Any idea?

Thanks for the support!

@zohararad
Copy link
Owner

@lghamie yes - read the docs carefully - If you need to modify the endpoint, you actually have to define your own before hooks, without using the default ones. This is due to an annoyance with Superagent, that requires defining the HTTP end-point first so it can be modified.

You can try and do something like that:

var hooks - require('sails-rest/lib/hooks'), createEndpoint;
hooks.before.pop(); //this removes the createEndpoint hook

createEndpoint = function(req, method, config, conn){
  // do your magic here
}

hooks.before.push(createEndpoint);

... later on, use hooks in your config, and set `merge: false`

Does that make sense?

@frontendgineer
Copy link

@zohararad
Many thanks for the sails-rest adapter.
Where do I find the docs regarding creating custom endpoint as you mentioned earlier?
I would like to integrate drupal 7 with sails.js. Drupal has its proprietary endpoints.

Many thanks in advance.

@zohararad
Copy link
Owner

@frontendgineer take a look at the code and the example above. My suggesting is to run sails-rest with a debugger a couple of times so you can see the flow of before and after hooks, and then you can modify at will.

If you need more help, post an example of your code and desired endpoint and i'll see if I can provide a more concrete example

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

No branches or pull requests

3 participants