How to make HTTPS requests with serverside javascript using Worklight?
Asked Answered
L

2

8

I'm toying around with IBM worklight, and am trying to create an adapter to feed some data in from the Google places API.

I want to call this URL :

https://maps.googleapis.com/maps/api/place/search/json?key=AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM&location=52.0700,1.1400&radius=10000&sensor=false&name=coffee

Executing this URL works fine in a browser, and displays some nice JSON that I'm trying to obtain via Worklight.

The Worklight adapters are created in Javascript, this is what I have so far :

function getCoffeeHouses() {

    var input = {
        method : 'get',
        returnedContentType : 'json',
        path : 'maps/api/place/search/json',
        parameters : {
            'key'       :   'AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM',
            'location'  :   '52.0700,1.1400',
            'radius'    :   '10000',
            'sensor'    :   'false',
            'name'      :   'coffee' 
        }
    };

    var response = WL.Server.invokeHttp(input);

 // Extract latitude and longitude from the response.
    var type = typeof response; 
    if ("object" == type) {
        if (true == response["isSuccessful"]) {
            // Return JSON object with lat and lng.
            return response["results"];
        } 
        else {
            // Returning null. Web request was not successful.
            return null;
        }
    } 
    else {
        // Returning null. Response is not an object.
        return null;
    }
}

And this is the result that I get in the console, when I test the above:

Failed to parse JSON string
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
  </style>
  <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/maps/api/place/search/json?key=AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM&amp;location=52.0700%2C1.1400&amp;radius=10000&amp;sensor=false&amp;name=coffee</code> was not found on this server.  <ins>That’s all we know.</ins>
Caused by: java.io.IOException: Unexpected character '<' on line 1, column 1
[2012-07-23 11:08:57] An error occurred while invoking procedure CoffeeFinder/getCoffeeHouses parameters: {
   "arr": [
   ]
}
null
Caused by: null

I think, that this is probably caused because the adapter is requesting as HTTP, whereas it should be using HTTPS.

If I alter the request to use HTTP in a browser, it displays similar results.

Question : Can I make an HTTPS request by altering the above Javascript, or am I misunderstanding worklight adapters?

Lonesome answered 23/7, 2012 at 10:49 Comment(0)
F
7

Looks like googleapis will not work if you don't specify Host header inside of your request. After adding it everything works as it should:

This is adapters's XML section

<connectivity>
    <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
        <protocol>https</protocol>
        <domain>maps.googleapis.com</domain>
        <port>443</port>            
    </connectionPolicy>
    <loadConstraints maxConcurrentConnectionsPerNode="2" />
</connectivity>

This is adapter's JS:

function doGet() {

var input = {
    method : 'get',
    returnedContentType : 'json',
    path : 'maps/api/place/search/json',
    headers: {
        Host: 'maps.googleapis.com'
    },
    parameters : {
        'key'       :   'AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM',
        'location'  :   '52.0700,1.1400',
        'radius'    :   '10000',
        'sensor'    :   'false',
        'name'      :   'coffee' 
    }
};

var response = WL.Server.invokeHttp(input);
return response;

}

Frum answered 24/7, 2012 at 8:48 Comment(2)
i too have a similar problem.. i mentioned all most all content types but its not working at all..Maintop
The error description is "The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type)" .... i have specified the Content-Type : 'application/json'.. but still the issue presist..Maintop
P
2

in your adapter there is also an {ADAPTER NAME}.xml

In it, under connectivity under connectionPolicy, there is the protocol. did you change it to https and deploy?

Postimpressionism answered 23/7, 2012 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.