Expand my Community achievements bar.

SOLVED

Bad Request Error when invoking third party Restful web services via .json

Avatar

Level 4

Hi Experts,

We are creating Adobe Experience Manager bundles that invoke third party Restful web services.

To create the AEM client that displays data returned by the Restful service, created these files:

  • query.json.jsp: that contains application logic that calls the OSGi bundle's getDistance method.
  • component.jsp: that contains application logic that parses the JSON data and displays data returned by the getDistance method. 

Now, When trying to excute it then it is throwing the BAD Request. Do we need to do any configuration for it?

GET http://localhost:4502/content/aemexample/en/restwebservicetest/_jcr_content.query.json 

Attached screen of errors

 

Thanks

~S

1 Accepted Solution

Avatar

Correct answer by
Level 10

Did you see our example that successfully invokes a Google Restful Service. 

try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
              
            HttpGet getRequest = new HttpGet("http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver%20BC&destinations=San%20Fr...");
            getRequest.addHeader("accept", "application/json");
 
            HttpResponse response = httpClient.execute(getRequest);
 
            if (response.getStatusLine().getStatusCode() != 200) {
                            throw new RuntimeException("Failed : HTTP error code : "
                                    + response.getStatusLine().getStatusCode());
                        }
 
            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
 
            String output;
             String myJSON="" ;
                while ((output = br.readLine()) != null) {
                    //System.out.println(output);
                    myJSON = myJSON + output;
                }
 
              
            httpClient.getConnectionManager().shutdown();
            return myJSON ;
        }

It uses Java HTTP API within a bundle and then displays the results in a JSP. (we will update this at some point to show usage within SIghtly). 

https://helpx.adobe.com/experience-manager/using/restful-services.html

You can install the package and invoke it to see the result. 

View solution in original post

6 Replies

Avatar

Correct answer by
Level 10

Did you see our example that successfully invokes a Google Restful Service. 

try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
              
            HttpGet getRequest = new HttpGet("http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver%20BC&destinations=San%20Fr...");
            getRequest.addHeader("accept", "application/json");
 
            HttpResponse response = httpClient.execute(getRequest);
 
            if (response.getStatusLine().getStatusCode() != 200) {
                            throw new RuntimeException("Failed : HTTP error code : "
                                    + response.getStatusLine().getStatusCode());
                        }
 
            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
 
            String output;
             String myJSON="" ;
                while ((output = br.readLine()) != null) {
                    //System.out.println(output);
                    myJSON = myJSON + output;
                }
 
              
            httpClient.getConnectionManager().shutdown();
            return myJSON ;
        }

It uses Java HTTP API within a bundle and then displays the results in a JSP. (we will update this at some point to show usage within SIghtly). 

https://helpx.adobe.com/experience-manager/using/restful-services.html

You can install the package and invoke it to see the result. 

Avatar

Level 10

I just tested on AEM 6.1 - it works:

Install the package and run it. It works. 

If you have issues - we can setup a connect session. 

Avatar

Level 4

Yes Actually I am following that example. When I downloaded the packages and installed then it is working fine. However when created the same bundle by using the maven eclipse and installed it. but when hitting the url then it is throwing the error like bad request. I don't know What I am missing.

Thanks

~S

Avatar

Level 10

Compare your project with the package project - it could be anything such as the JSPs are not at the same node level as the main component JSP or  a copy and paste error was made. This is exactly why we package up the HELPX articles so community members can run them and compare to their projects if they follow along. Most of the time - its a minor error.  

Avatar

Level 4
Yes, please schedule the session. I can share my screen and show you .
I am getting below two error:
 
1. Failed to load resource: the server responded with a status of 404 (Not Found) on URL
2. When I am hitting this url separatly then getting below error

Invalid recursion selector value 'query'

Cannot serve request to /content/versiontenexample/en/testrest/_jcr_content.query.json in org.apache.sling.servlets.get.DefaultGetServlet

Avatar

Level 10

Yes - we will hold it weds mar 2.