Support Topics Documentation Slack YouTube Blog

REST API End Point


(mike turner) #1

Pretty new to the API Services on the Business Logic side on Backendless so forgive any wrong terminology!

Following Marks (excellent) video made good headway beginning an api.

I can successfully run a GET request like this…
https://api.backendless.com/xxx/xxx/services/APIService/myCustomers?id=3

and my corresponding simple code in the business logic code area is this.
/**

  • @route GET /myCustomers
  • @param {int} id
    */
    helloCustomer( id ){
    return "hello customer id: " + id;
    }

but what I can’t achieve is getting the ID without using a parameter in the URL. I need this as I have to produce an endpoint more like this (as requested by another 3rd party app)
https://api.backendless.com/xxx/xxx/services/APIService/myCustomers/3

Is this possible and if so could you show me the code?

Many thanks.

Mike


(Vladimir Upirov) #2

Hi Mike
Yes, it’s possible, you can override request method and request route as well
here is a doc for that https://backendless.com/docs/bl-js/bl_rest_routes.html

and a simple example:

'use strict';

class SampleService {
  /**
   * @route GET /my-custom-route/items/{itemId}
   * @returns {Object}
   */
  getItem() {
    const { itemId } = this.request.pathParams
    
    return { 
      itemId
    }
  }
}

Backendless.ServerCode.addService(SampleService);


(mike turner) #3

Excellent, thanks Vladimir.

That works perfectly, just what I wanted.


(Vladimir Upirov) #4

good, glad to hear it