Bad Request Error when invoking third party Restful web services via .json | Community
Skip to main content
Level 4
February 26, 2016
Solved

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

  • February 26, 2016
  • 6 replies
  • 2587 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

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%20Francisco&sensor=false");
            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. 

6 replies

smacdonald2008
smacdonald2008Accepted solution
Level 10
February 26, 2016

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%20Francisco&sensor=false");
            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. 

smacdonald2008
Level 10
February 26, 2016

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. 

Level 4
February 26, 2016

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

smacdonald2008
Level 10
February 26, 2016

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.  

Level 4
March 1, 2016
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
smacdonald2008
Level 10
March 1, 2016

Yes - we will hold it weds mar 2.