API gateway POST passing fields and throwing error - node.js

I have an API Gateway for a POST request which calls a lambda function.
When I test the Lambda function, it works
When I test the endpoint in the API Gateway, it works.
However, When I call the function via a CURL request or via a website, I get the following error:
{"message": "Could not parse request body into json: Could not parse payload into json: Unrecognized token \'eyJtZXNzYWdlIjoiSSBsaWtlIGdpcmxzIGJ1dCBoYXZlIGEgd2lmZSBzbyBuZWVkIHRvIGJlIGRpc2NyZWV0LiBwcy4gSSBoYXZlIGEgYmlnIGNvY2sgOnAifQ\':
was expecting (JSON String, Number, Array, Object or token \'null\', \'true\' or \'false\')\n at [Source: (byte[])\"eyJtZXNzYWdlIjoiSSBsaWtlIGdpcmxzIGJ1dCBoYXZlIGEgd2lmZSBzbyBuZWVkIHRvIGJlIGRpc2NyZWV0LiBwcy4gSSBoYXZlIGEgYmlnIGNvY2sgOnAifQ==\"; line: 1, column: 124]"}%
When receiving that error, the call never makes it to my lambda function. So something in the API Gateway is blocking my request.
My CURL request looks like
curl -i --location --request POST 'https://example.com/staging/transform' 'Content-Type: application/json' --data-raw '{"message":"test"}'
What could be the culprit here?

You need to specify 'Content-Type: application/json' as header with -H option.
curl -i --location -H 'Content-Type: application/json' --request POST 'https://example.com/staging/transform' --data-raw '{"message":"test"}'

Related

Calling API through requests Library to get access token

I am trying to get the access token for a specific API, the curl command is given below:
curl --location --request POST 'https://example.com/oauth/connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=abcd' \
--data-urlencode 'client_secret=abc' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=us' \
--data-urlencode 'scope=a' \
--data-urlencode 'password=abc'
I have the following code in python:
url = 'https://example.com/oauth/connect/token'
body = {'client_id':'abcd',
'client_password':'abc',
'grant_type':'password',
'username':'us',
'scope':'a',
'password':'abc'}
headers = {'Content-Type':'application/x-www-form-urlencoded'}
response = requests.post(url, data=body, headers=headers, verify=False)
print(response,response.content)
I keep receiving a <Response [400]> b'{"error":"invalid_client"}' error.
I believe I have tried almost everything there is out there and I am lost. Please let me know if I am missing something really simple.
I noticed, that your curl command uses client_secret, while your requests command uses client_password.
For debugging such requests, I recommend free pages like https://www.toptal.com/developers/postbin/, where you can send the curl and the requests message and compare what arrived on the endpoint.

excel webservice http header

I am wondering if there is a way to include http header in excel WEBSERVICE function.
I am trying to use an API witch need a Authorization: Bearer ACCESS_TOKEN :
curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer ac004670-8f66-340a-b4d0-312b585d8cb3' 'https://api.insee.fr/metadonnees/V1/codes/nafr2/sousClasse/84.11Z'

How to use azure translator with authorization token?

I want to use azure translator service with the Authorization token. There are plenty of resources explaining how to do it with subscription key but I could not found any resource explaining the use of authorization token. I can get the authorization token but when I send a request with it, the response status code is 401.
This is how I send the request:
curl POST 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=es' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer token' \
--data-raw '[{'\''Text'\'':'\''Hello World!'\''}]'
If you want to call Text Translation API with authentication token, please refer to the following steps
Get a token. If your service is global, the endpoint is https://api.cognitive.microsoft.com/sts/v1.0/issueToken
curl -v -X POST \
"https://YOUR-REGION.api.cognitive.microsoft.com/sts/v1.0/issueToken" \
-H "Content-type: application/x-www-form-urlencoded" \
-H "Content-length: 0" \
-H "Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY"
Besides, please note that an authentication token is valid for 10 minutes. The token should be reused when making multiple calls to the Translator. However, if your program makes requests to the Translator over an extended period of time, then your program must request a new access token at regular intervals (for example, every 8 minutes).
Call API
curl -X POST 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=de' \
-H 'Authorization: Bearer YOUR_AUTH_TOKEN' \
-H 'Content-Type: application/json' \
--data-raw '[{ "text": "How much for the cup of coffee?" }]' | json_pp
For more details, please refer to here and here.

Testing AWS API Gateway with cURL

I do have a simple AWS API Gateway implementation protected by an AWS_IAM Authorization.
I just want to test from command line via cURL :
curl --location --request GET 'https://<API_ID>.execute-api.eu-west-1.amazonaws.com/stage?type=type&category=category&lc=lc&passprhase=passprhase&product=product'
--header 'Authorization: AWS4-HMAC-SHA256 Credential=<AWS_ACCESS_KEY>/20200127/eu-west-1/execute-api/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=<AWS_SECRET_ACCESS_KEY>' --header 'Content-Type: application/json' \
--data-raw '{"query":"","variables":{}}'
but keep getting the follow error :
Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header.
Can someone advice what am I doing wrong ?
AWS V4 signature authentication is supported in curl starting from version 7.75, so you should be able to call your AWS resource this way:
curl --location --request GET 'https://$API-ID.execute-api.eu-west-1.amazonaws.com/stage?type=type&category=category&lc=lc&passprhase=passprhase&product=product' \
--header 'Content-Type: application/json' \
--user $ACCESS_KEY:$SECRET_KEY \
--aws-sigv4 "aws:amz" \
--data-raw '{"query":"","variables":{}}'
Note that you may need to add in the --aws-sigv4 value your region and service.
For example: --aws-sigv4 "aws:amz:eu-west-2:execute-api"
You can find more documentation here: https://curl.se/libcurl/c/CURLOPT_AWS_SIGV4.html
And the documentation for the CLI option here: https://curl.se/docs/manpage.html#--aws-sigv4
AWS_IAM authorization uses Sigv4 and its calculation process requires values certain headers - Date being one of them. You are passing x-amz-date as a part of the "SignedHeaders" field, but not actually passing it with the other headers.
One way to create the right curl command to invoke an API with AWS_IAM would be to use Postman application. Add in the API URL and select "AWS Signature" under Authorization tab. You can then select the "Code" option and get the full curl command which would look something like this -
curl -X POST \
https://$API-ID.execute-api.$AWS_REGION.amazonaws.com/$STAGE/$RESOURCE \
-H 'authorization: AWS4-HMAC-SHA256 Credential=$ACCESS_KEY/20200128/$AWS_REGION/execute-api/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=$SIGNATURE_VALUE' \
-H 'cache-control: no-cache' \
-H 'content-type: application/x-www-form-urlencoded' \
-H 'host: API-ID.execute-api.$AWS_REGION.amazonaws.com' \
-H 'postman-token: 15f9498e-95b7-f22b-eed9-016cdea07424' \
-H 'x-amz-date: $DATE_STAMP'
Create a Canonical Request for Signature Version 4
I could suggest to use awscurl which is much easier.
To install awscurl click here. For documentation you can refer here.
Example to call apigateway to call lambda for POST query is below.
awscurl --service execute-api -X POST -d '{ "alias" : "xyx", "type" : "LDAP" }' https://.execute-api.us-west-2.amazonaws.com/Prod/user/groups/get --region us-west-2 --access_key ACCESS_KEY --secret_key mfBl0YJRsXDue4C5F5B6rz1eUpQpA8uC24RtSnsg --security_token SECURITY_TOKEN

Not receiving push notification with encrypted payload

I've been following the document over in this resource to get Push-Notifications sending to a browser with a payload. (Chrome 53.0.2785.116 m).
I'm using the Nodejs Crypto library to generate the keys and they all pass the verification test created by the folks at Google.
I can send and receive a Push-Notification when I do not include the encrypted payload. As soon as I attach the 'raw_data' tag with the encrypted payload the browser no longer receives the Push-Notification (although Curl still comes back with a 'success(1)' response)
Below is the Curl message with and without the payload.
Without Payload -
curl --header "TTL:86400"
--header "Authorization: key=API_KEY"
--header "Encryption: salt=E2oUmcFWbb41wk5DuTbuoA"
--header "Crypto-Key: dh=SERVER_PUBLIC_KEY"
--header "Content-Encoding: aesgcm"
--header "Content-Type: application/json"
https://android.googleapis.com/gcm/send -d
"{\"registration_ids\":[\"CLIENT_KEY\"],\"content_available\":true}"
--insecure
With Payload -
curl --header "TTL:86400"
--header "Authorization: key=API_KEY"
--header "Encryption: salt=E2oUmcFWbb41wk5DuTbuoA"
--header "Crypto-Key: dh=SERVER_PUBLIC_KEY"
--header "Content-Encoding: aesgcm"
--header "Content-Type: application/json"
https://android.googleapis.com/gcm/send -d
"{\"registration_ids\":[\"CLIENT_KEY\"],\"raw_data\":\"UnGSxqxfy9yDwPgCVeXJibQyPX8Hz28\",\"content_available\":true}"
--insecure
Both return a success message
{"multicast_id":9197002368280649941,
"success":1,"failure":0,
"canonical_ids":0,
"results":[{"message_id":"0:1474618927798802%bc86b90cf9fd7ecd"}]}
but the latter is not received by the browser.
We're now into the fourth day of head scratching. Any help would be great.

Resources