Search code examples
restibm-cloud

How to access REST api endpoints for Bluemix


I followed an IBM Blumix article on "Build and deploy a REST API on IBM Bluemix with PHP and MySQL". http://www.ibm.com/developerworks/library/wa-deployrest-app/index.html

However it never says how to access the actual endpoints(I think I am using the right word). It says to call "/v1/products" to list all products. I am very new to bluemix and not too sure exactly what to append "/v1/products" to. Should I append it to make ">projectName<.mybluemix.net/v1/products"? This is not working. I get the error message

"Not Found

The requested URL /v1/products was not found on this server."

Cheers


Solution

  • If you have followed the developerWorks article then you should just be able to access the endpoint by doing a GET call (i.e. just adding this to a web browser) this url:

    your_app_route.mybluemix.net\v1\products

    or if you deployed to the UK (eu-gb) region then the url is:

    your_app_route.eu-gb.mybluemix.net\v1\products

    then append either .xml or .json to that url for the desired response type. The endpoint is defined in your app code, and from reading the developerWorks article it is set in the PHP code at this point:

     $app->path('v1', function($request) use ($app) {
    
        $app->path('products', function($request) use ($app) { 
    
    
        // GET /v1/products[.xml|.json]
        // list all products
        $app->get(function() use ($app)  {
    
          $products = Product::all();         
    
          // handle requests for XML content
          $app->format('xml', function($request) use($app, $products) {
            return $app->response(200, convert_array_to_xml($products->toArray()))
                          ->header('Content-Type', 'application/xml');
          });
    
          // handle requests for JSON content
          $app->format('json', function($request) use($app, $products) {
            return $products->toArray();
          });
    

    The your_app_route is the route/hostname name you can define when you do the cf push command, if you don't explicitly set this route (-n option or --random-route option) then it will be set to the name of your Bluemix application. You can see what the url is by looking at the Bluemix UI console for your app (should be at the top of the page) or by looking at the end of the cf push command where is says urls:

    From the error you are getting it sounds like the app is starting, but the web server is not directing the requests to the Bullet module. You might want to check that Step 8(b) has been done correctly.