AWS Lambda python function giving json headers in the output - json

I am trying to invoke lambda service. When I hit the Get method(under Api gateway->stages->GET) invoke Url I see json headers and status code also. But in the acloud guru lecture video, I see just the body. Can anyone please tell what am I missing here.
Here is my python function:
def lambda_handler(event, context):
print("In lambda handler")
resp = {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*",
},
"body": "myName"
}
return resp
Actual Output:
{"statusCode": 200, "headers": {"Access-Control-Allow-Origin": "*"}, "body": "myName"}
Expected output:
myName

here you have used lambda proxy integration and did not enable it in API gateway level.
You can enable it under Integration request, see the image below
There are 2 types of API Gateway and Lambda integration
Proxy Integration - Request to API gateway is directly forwarded to lambda and response is sent from lambda. we have to create the response body with appropriate status code and headers inside the lambda in this integration
Lambda Integration - request can be modified before sending to lambda and response can be modified from lambda response in API gateway level using mapping templates
This blog post gives more details about the 2 integrations
https://medium.com/#lakshmanLD/lambda-proxy-vs-lambda-integration-in-aws-api-gateway-3a9397af0e6d

Related

How to allow Cors Headers in Google AppScript for making an XMLHttpRequest?

I have created a doGet and doPost endpoints in my appscript. When I hit the endpoint to make a post request from Python, it does work perfectly and as expected.
But when I try to hit the same url with my Flutter based mobile App, it throws me an XML error. (Which I suspect is related to CORSING).
When I hit the url with get request, I get the right response, but post request is failing. To ensure that my Post request is properly configured, I have made a post request to public API and it worked like charm.
Is it possible to add headers, where I could enable cors like this:
allowHeaders = {
"Access-Control-Allow-Origin": "*", // Required for CORS support to work
"Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS
"Access-Control-Allow-Headers": "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
"Access-Control-Allow-Methods": "POST, OPTIONS"
}
Here is what my doPost request returns:
ContentService.createTextOutput(JSON.stringify(
{
data: isAuthenticated.data,
error: true,
//request: request,
msg: query.apiKey,
//paramters:request.parameters
})).setMimeType(ContentService.MimeType.JSON)
Here is my python script to get the post response:
requests.post("https://script.google.com/macros/s/AKfycbz7kTROol8u509M_p9pMZ9XRnL-myVjcRQKeb9Etp_OIMPnH640vHf_0Jp2dvvrbto7kOg/exec",
json = requestObject)
And here is my Flutter function:
Future<http.Response> createAlbum() async{
print("Trying to make a post request");
var result = await http.post(Uri.parse('https://script.google.com/macros/s/AKfycbz7kTROol8u509M_p9pMZ9XRnL-myVjcRQKeb9Etp_OIMPnH640vHf_0Jp2dvRIco7kOg/exec'),
headers: {"Content-Type": "application/json"},
body: jsonEncode(<String, dynamic>{
"apiKey":apiKey,
"operationType":"register_user",
"operationData": {
"email": "shivam#yoptima.com",
"otp": 318728
}
}),
);
print("Here is the result: " + result.body);
}
Just to clarify things:
Get Request works for both the platforms.
Post Request works with python for AppScript.
Post Request works for any other public API from flutter.
Post Request doesn't work for Flutter when Hitting AppScript API.
I suspect it to be something to do with CORS. (But not very sure).
Flutter http library makes request via XMLHttpRequest.

Accessing user token in IBM Cloud Functions serverless app secured with OAuth user authentication

I am creating a serverless app using IBM Cloud Functions. My Cloud Functions API is secured with OAuth user authentication using an IBM Cloud App ID service. When a user logs into my app, an access token is generated by this service. I want to extract user data from that access token so that I can customize the user experience.
How do I access that token from within a Cloud Functions action that is coded for Node.js 10?
Example
The openwhisk webaction doc
https://github.com/apache/openwhisk/blob/master/docs/webactions.md
states that the following code
function main(params) {
return { response: params };
}
generates the following response
{
"response": {
"__ow_method": "get",
"__ow_headers": {
"accept": "*/*",
"connection": "close",
"host": "172.17.0.1",
"user-agent": "curl/7.43.0"
},
"__ow_path": ""
}
}
From that data I should be able to get HTTP request details. Specifically, I should be able to get the Authorization header value off the "__ow_headers" property of the action argument (params).
However, the same code inside an IBM Cloud Functions web action generates nothing. Nothing exists on the params object.

how can i call HTTP callable cloud function from postman?

I want to know that can i call cloud function from postman software .When i'm calling CF from postman it always give me
"error": {
"status": "INVALID_ARGUMENT",
"message": "Bad Request"
}
In Postman make post request, header Content-Type should be application/json and then in raw make json in this format
{
"data": {
"text":"hi how are you",
"phoneNumbers":"+92123455679"
}
}
Using #Vaaljan's answer I was able to authenticate a GCP cloud function that I created using the HTTP trigger method with authentication required:
gcloud auth print-identity-token
Then added it to the request Authorization header:
Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6I ... bnRzLmdvb2dsZS5jb20i
In Postman:
Could possibly also be that the cloud function is protected and can be invoked by adding Authorization header with the identity token as the Bearer.
You get the identity token by running
gcloud auth print-identity-token

How to determine why an Azure Function App is not triggered by a webhook

I have:
An JavaScript Azure Function in an HTTP webhook configuration; the Function provides a URL; the Function performs an action
A webhook configured in the software I hope to receive notifications from
An Azure Logic App with an HTTP/webhook step that provides a URL for the webhook notification to go to
My goal is that the Azure Function's URL receives notifications from the software's webhook and performs an action. The Azure Logic App is for testing only.
What works
When the the Azure Logic App's URL is used in the software's webhook configuration, the desired action is performed. All works as expected.
The Azure Logic App's logging shows the JSON output from the incoming webhook. I expect (but believe this may be where I am going wrong) that this is the JSON the webhook is sending to the Azure Logic App's URL. When this JSON is used in the Azure Function UI's "Test" tab > "Request body" field, the desired action is performed. All works as expected.
When the Azure Function's URL and the JSON is in a Postman request, the desired action is performed. All works as expected.
What doesn't work
When the Azure Function's URL is used in the software's webhook configuration, no action is performed. This is of course my goal. From everything I have read, I understand that this URL as a webhook endpoint should work.
Azure Function's URL
This is from Get function URL > default (Function key).
https://<app_name>.azurewebsites.net/api/content?code=<api_key>
Other Azure Function config settings
Allowed HTTP methods: GET, POST
Authorization level: Function
The JSON I believe to be coming over the webhook
{
"headers": {
"Expect": "100-continue",
"Host": "redacted",
"X-Telligent-Webhook-Sender": "redacted",
"Content-Length": "16908",
"Content-Type": "application/json; charset=utf-8"
},
"body": {
"events": [{
"TypeId": "ec9da4f4-0703-4029-b01e-7ca9c9ed6c85",
"DateOccurred": "2018-12-17T22:55:37.7846546Z",
"EventData": {
"ActorUserId": 9999,
"ContentId": "redacted",
"ContentTypeId": "redacted",
"ForumReplyId": 9999,
"ForumThreadId": 9999,
"ForumId": 9999
}
}]
}
}
I also tried with the following test code for the same results. It aligns more closely with the sample payload data provided by the software company:
What I tried
{
"events": [{
"TypeId": "ec9da4f4-0703-4029-b01e-7ca9c9ed6c85",
"DateOccurred": "2018-12-17T22:55:37.7846546Z",
"EventData": {
"ActorUserId": 9999,
"ContentId": "redacted",
"ContentTypeId": "redacted",
"ForumReplyId": 9999,
"ForumThreadId": 9999,
"ForumId": 9999
}
}]
}
Sample payload data
{
"events": [
{
"TypeId": "407ad3bc-8269-493e-ac56-9127656527df",
"DateOccurred": "2015-12-04T16:31:55.5383926Z",
"EventData": {
"ActorUserId": 2100,
"ContentId": "4c792b81-6f09-4a45-be8c-476198ba47be"
}
},
{
"TypeId": "3b75c5b9-4705-4a97-93f5-a4941dc69bc9",
"DateOccurred": "2015-12-04T16:48:03.7343926Z",
"EventData": {
"ActorUserId": 2100,
"ContentId": "4c792b81-6f09-4a45-be8c-476198ba47be"
}
}
]
}
I do not know how to determine why the Azure Function is not triggered by the webhook. The software's API documentation does not seem to provide a way to look at the JSON being sent over the webhook, although in my inexperience I may be wrong.
Is there a mechanism within Azure, or Postman, or another tool that lets me see what JSON is being sent over the webhook? Or perhaps is there another approach to determining the cause of the issue?
Thank you for any help.
This is how I got the JSON file from Azure alerts.
Install Ruby on the server
Install Sinatra with following command gem install sinatra
Create file webhook.rb and paste code bellow
require 'sinatra'
set :port, 80
set :bind, '0.0.0.0'
post '/event' do
status 204 #successful request with no body content
request.body.rewind
request_payload = JSON.parse(request.body.read)
#append the payload to a file
File.open("events.txt", "a") do |f|
f.puts(request_payload)
end
end
Run the web service with command ruby webhook.rb
JSON fill be written to file events.txt

AWS API Gateway Malformed Lambda Response

The documentation states that the json should return containing a body,headers,and a status code all of which I have. However for whatever reason when I test it in API gateway it returns a malformed response.
This is the output of the method below it.
"{\"body\": 200, \"headers\": {\"Content-type\":
\"application/json\"}, \"statusCode\": 200}"
def addnumbers(message, context):
result = message['num1'] + 1
print(result)
resp = {
"statusCode": 200,
"body": result,
"headers": { "Content-type": "application/json"}
}
return (json.dumps(resp))
I am currently passing in num1=1 and it doesn't give any better error message. Any guidance would be appreciated.
Ok buckle in for an answer.
Make sure you have proxy integration enabled on whatever resource you want in your API.
Now go to your lambda. Look at how I was previously trying to pass in num1.I was trying to get it from the "Event" or message. This is where I was tripping up. Also note (you can't do a get with a body)
Rather the input to the lambda should be like this.
{
"queryStringParameters": {
"input": "Whatever the input is you want the lambda to test"
}
}
So now that we have our test configured for the lambda we need to code the lambda itself.
I put this code within :
def lambda_handler(event, context):
number = "Hello, " + event['queryStringParameters']['input']
out = {}
out['statusCode'] = 200
out['body'] = number
return (out)
Now if you test it should be fine.
Go back to the API Gateway
In the "Query Strings" Section put in input=randomname
It should now return with hello, randomname