如何使用Worklight使用serverside javascript发出HTTPS请求? [英] How to make HTTPS requests with serverside javascript using Worklight?
问题描述
我正在使用 IBM worklight ,我正在尝试创建一个适配器从 Google地方信息图中提供一些数据。
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
执行此URL在浏览器中工作正常,并显示我试图通过Worklight获取的一些不错的JSON。
Executing this URL works fine in a browser, and displays some nice JSON that I'm trying to obtain via Worklight.
Worklight适配器是在Javascript,这是我到目前为止:
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&location=52.0700%2C1.1400&radius=10000&sensor=false&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
我认为,这可能是因为适配器请求为HTTP,而应该使用HTTPS。
I think, that this is probably caused because the adapter is requesting as HTTP, whereas it should be using HTTPS.
如果我在浏览器中更改了使用HTTP的请求,它会显示类似的结果。
If I alter the request to use HTTP in a browser, it displays similar results.
问题:我可以通过更改上述Javascript来发出HTTPS请求,还是我误解了worklight适配器?
推荐答案
如果您未在请求中指定主机标头,则看起来像googleapis将无效。
添加后,一切正常:
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:
这是适配器的XML部分
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>
这是适配器的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;
}
这篇关于如何使用Worklight使用serverside javascript发出HTTPS请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!