[英]Aws LAMBDA : HTTP POST request
I'm trying to post data from a the OData RESTful API from a sample service used which is the TripPin. 我正在尝试使用来自TripPin的示例服务中的OData RESTful API发布数据。 The request succeeded but the response est null.
请求成功,但响应est为空。 Is there any particular structure to respect in AWS Lambda platform in order to make HTTP post request?
为了发出HTTP发布请求,AWS Lambda平台中是否有任何特定的结构要遵守?
var querystring = require('querystring');
var http = require('http');
exports.handler = function (event, context) {
var post_data = querystring.stringify(
{
UserName:'lewisblack',
FirstName:'Lewis',
LastName:'Black',
Emails:[
'lewisblack@example.com'
],
AddressInfo:[
{
Address: '187 Suffolk Ln.',
City: {
CountryRegion: 'United States',
Name: 'Boise',
Region: 'ID'
}
}
],
Gender: 'Male'
}
);
// An object of options to indicate where to post to
var post_options = {
host: event.url,
port: '80',
path: event.path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
console.log("hello");
context.succeed();
});
res.on('error', function (e) {
console.log("Got error: " + e.message);
context.done(null, 'FAILURE');
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
Example of Call Parameters : 调用参数示例:
{
"url": "services.odata.org",
"path": "/v4/TripPinServiceRW/"
}
Response: null 回应:空
Request ID: "6f1ec2b4-5195-477f-9fb8-56fd33dee0ce" 请求ID:“ 6f1ec2b4-5195-477f-9fb8-56fd33dee0ce”
Function Logs: START RequestId: 6f1ec2b4-5195-477f-9fb8-56fd33dee0ce Version: $LATEST 功能日志:START RequestId:6f1ec2b4-5195-477f-9fb8-56fd33dee0ce版本:$ LATEST
END RequestId: 6f1ec2b4-5195-477f enter code here
-9fb8-56fd33dee0ce END RequestId:6f1ec2b4-5195-477f
enter code here
-9fb8-56fd33dee0ce
REPORT RequestId: 6f1ec2b4-5195-477f-9fb8-56fd33dee0ce Duration: 431.87 ms REPORT RequestId:6f1ec2b4-5195-477f-9fb8-56fd33dee0ce持续时间:431.87 ms
Billed Duration: 500 ms Memory Size: 128 MB Max Memory Used: 73 MB 计费时间:500 ms内存大小:128 MB使用的最大内存:73 MB
TL;DR; TL; DR; I think your AWS Lambda integration is broken, but it's hard for me to know for sure.
我认为您的AWS Lambda集成已损坏,但是我很难确定。 Apologies for the lecture.
致歉的演讲。
I recommend the following: 我建议以下内容:
What you get in the event
object depends on how you are calling your AWS Lambda function. event
对象中的输出取决于您如何调用AWS Lambda函数。 You can invoke it directly via an API call , or indirectly by having another AWS service trigger it. 您可以通过API调用直接调用它,也可以通过让另一个AWS服务触发它来间接调用它。 Note that:
注意:
The structure of the event document is different for each event type, and contains data about the resource or request that triggered the function
每种事件类型的事件文档结构都不同,并且包含有关触发该函数的资源或请求的数据
https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html
The easiest way to find out would probably be to add some logging at the start of your handler: 最简单的查找方法可能是在处理程序的开头添加一些日志记录:
console.log(JSON.stringify(event));
Once you've figured out what's actually in the event
object, write a test for it . 一旦弄清了
event
对象的实际含义,就为它编写一个测试 。 This should improve both the design of your code and your development cycle. 这将改善代码的设计和开发周期。
Testing the code above against a local test server gives a successful response. 在本地测试服务器上测试以上代码将获得成功的响应。 See below.
见下文。
But that doesn't mean it will work against https://services.odata.org . 但这并不意味着它将与https://services.odata.org兼容 。
Again, you should figure out how you should call that service and write a test for it. 同样,您应该弄清楚如何调用该服务并为其编写测试。
index.spec.js index.spec.js
var index = require("./index");
describe('index', () => {
it('can make requests to localhost', (done) => {
return index.handler({
"url": "localhost",
"path": "/"
}, {
done: done,
succeed: done
});
});
});
package.json package.json
...
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^24.8.0"
}
...
Output: 输出:
> npm test
> lambda-post@1.0.0 test /somepath/index.spec.js
> jest
PASS ./index.spec.js
index
✓ can make requests to localhost (20ms)
console.log index.js:44
Response: <html><body><h1>POST!</h1></body></html>
console.log index.js:45
hello
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.89s, estimated 1s
Ran all test suites.
You're using a low level node http library. 您正在使用低级节点http库。 A library like axios may make your life easier in the short term.
像axios这样的库可能在短期内使您的生活更轻松。 See the HTTP Post example on their readme / wiki.
请参阅其自述文件/维基上的HTTP Post示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.