Appsync vtl template does not work with #set variable, but does with a hardcoded string? - aws-appsync

So the two templates below are identical except the first one uses a hard-coded json string for the "input" variable, and the latter uses a #set map variable wrapped with $util.toJson.
Should't they both have an equal result input variable for use in the lambda step finction?
So this template works as expected:
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/",
"params": {
"headers": {
"content-type": "application/x-amz-json-1.0",
"x-amz-target":"AWSStepFunctions.StartSyncExecution"
},
"body": {
"stateMachineArn": "arn:aws:states:us-east-2:712123061483:stateMachine:add-cognito-pool-user",
"input": "{ \"tenantId\": \"$context.identity.claims.name\" }"
}
}
}
This one does not:
#set( $myMap = {
"tenantId": $context.identity.claims.name
} )
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/",
"params": {
"headers": {
"content-type": "application/x-amz-json-1.0",
"x-amz-target":"AWSStepFunctions.StartSyncExecution"
},
"body": {
"stateMachineArn": "arn:aws:states:us-east-2:712123061483:stateMachine:add-cognito-pool-user",
"input": $util.toJson($myMap)
}
}
}
Why doesn't the latter work? It just behaves as if there is no "tenantId" variable set in my lambda step function.

Related

Wiremock verify - How to match the request with time parameter

I have time (messageId) parameter in my request. When i try to wiremock its not matching as expected. Please let me know how can we match like these scenarios
EX:
messageId = current date and time in millisec
com.github.tomakehurst.wiremock.client.VerificationException: No requests exactly matched. Most similar request was: expected:<
POST
/xxx/v2/yyy?apikey=test_key&messageId=1614515075245&calculatePromotions=false&origin=yy&siteCode=123&workstationId=0
but was:<
POST
/xxx/v2/yyy?apikey=test_key&messageId=1614515078010&calculatePromotions=false&origin=yy&siteCode=123&workstationId=0
You can match using regex on query parameters, which should solve your issue.
{
"request": {
"urlPath": "/xxx/v2/yyy",
"method": "POST",
"queryParameters": {
"api_key": {
"equalTo": "test_key"
},
"messageId": {
"matches": ".*"
},
"calculatePromotions": {
"equalTo": false
},
"origin": {
"equalTo": "yy"
},
"siteCode": {
"equalTo": 123
},
"workstationId": {
"equalTo": 0
}
}
},
"response": {
"status": 200
}
}

WireMock set optional Parameters possibly?

i am very new here. I look up to setup die optional parameters in my Pattern. I have already read the documentary WireMock, but I have not found anything suitable.
My question is, can I query the parameters in any order =?
The next one would is ,y caseInsensitive doesn't work. I dont know why.
{
"priority": 1,
"request": {
"method": "GET",
"headers": {
"Content-Type": {
"equalTo": "application/json",
"caseInsensitive": true
}
},
"urlPattern": "/example\\?name=([a-zA-Z0-9]*)&id=([a-zA-Z0-9]*)"
},
"response": {
"status": 200,
"bodyFileName": "example/test.json"
}
}
As you've written your urlPattern, the query parameter matching is not order indifferent. If you want the query parameters to indifferent, you'd need to do something like...
{
"priority": 1,
"request": {
"method": "GET",
"headers": {
"Content-Type": {
"equalTo": "application/json"
}
},
"urlPath": "/example",
"queryParameters": {
"name": {
"matches": "([a-zA-Z0-9]*)"
},
"id": {
"matches": "([a-zA-Z0-9]*)"
}
}
},
"response": {
"status": 200,
"bodyFileName": "example/test.json"
}
}
The result of the query comes back the same answer.
i want, if i call my request, that the order of Parameters doesn't matter.
example Request: /example?name=max&id=01
example2 Request: /example?id=01&name=max
it should be get same Response.
And it should be case-insensitive.

Wiremock standalone dynamic response array of objects is not working with bodyPatterns and matchesJsonPath

I am using wiremock to stubbing the requests. I have created a json file to get a response:
{
"request": {
"method": "POST",
"urlPath": "/nested/transform",
"bodyPatterns": [
{
"matchesJsonPath": "$.name.[0].first"
},
{
"matchesJsonPath": "$.name.[1].first"
}
]
},
"response": {
"status": 200,
"body": "{\"firstName\": \"$(name.[0].first)\", \"lastName\": \"$(name.[1].first)\"}",
"headers": {
"Content-Type": "application/json"
},
"transformers": ["body-transformer"]
}
}
My request and response are as below:
Request
{
"name": [
{
"first": "Vijay"
},
{
"first": "Sagar"
}
]
}
Here I receive very beard response and it is not parsed as I want.
Response which is not my expected result:
{
"firstName": "[{first=Vijay}, {first=Sagar}]",
"lastName": "[{first=Vijay}, {first=Sagar}]"
}
Expected result is: I'm willing to receive the following response based on above request and stubbed json:
{"firstName": "Vijay","lastName": "Sagar"}
How can I get the expected result as I tried a lot but was unable to match response parameters?
When working with a JSON response, I prefer to use the bodyFileName as this that escaping isn't necessary.
__files/nested_json_template.json
{
"firstName": "{{jsonPath request.body '$.name.[0].first'}}",
"lastName": "{{jsonPath request.body '$.name.[1].first'}}"
}
mappings/nested_json_mapping.json
{
"request": {
"method": "POST",
"urlPath": "/nested/transform",
"bodyPatterns": [
{
"matchesJsonPath": "$.name.[0].first"
},
{
"matchesJsonPath": "$.name.[1].first"
}
]
},
"response": {
"status": 200,
"bodyFileName": "nested_json.json",
"headers": {
"Content-Type": "application/json"
},
"transformers": ["response-template"]
}
}

HTTP request to Cloud-Convert not working

I am trying to use fetch to POST an API call to Cloud Convert and getting the following error:
message: "The given data was invalid."
code: "INVALID_DATA"
errors: {...}
tasks: Array(1)
0: "The tasks field is required."
Here is my code (on Wix):
export async function convertMp4toMp3(fileUrl, filename) {
filename = filename.replace(/mp4/gi, "mp3")
let job = {
tasks: {
"import-2": {
"operation": "import/url",
"url": fileUrl
},
"task-1": {
"operation": "convert",
"input_format": "mp4",
"output_format": "mp3",
"engine": "ffmpeg",
"input": [
"import-2"
],
"audio_codec": "mp3",
"audio_qscale": 0
},
"export-1": {
"operation": "export/google-cloud-storage",
"input": [
"task-1"
],
"project_id": "project-id",
"bucket": "bucket",
"client_email": "client_emailXXXXXXX",
"file": filename,
"private_key": "My Private Key
}
}
}
let options = {
"method": "POST",
"body": job,
"headers": {
"Authorization": "Bearer MyApiKey",
"Content-type": "application/json"
}
}
let response = await fetch("https://api.cloudconvert.com/v2/jobs", options)
console.log(response.json())
}
As you can see, the "tasks" field is populated with the jobs...
The fetch API does not automatically encode JSON.
Try:
let options = {
"method": "POST",
"body": JSON.stringify(job),
"headers": {
"Authorization": "Bearer MyApiKey",
"Content-type": "application/json"
}
}

Wiremock Capture path param and return in the response body

I am trying to create dynamic mocks using WireMock. I have a situation where if I specify URL like :
http://localhost:8089/api/account/abc#abc.com
then I should receive response like:
{
"account" : "abc#abc.com"
}
In brief, the path param is returned in the response body. I am able to make the request URL generic by using urlPathPattern set to /api/account/([a-z]*) however, I am not sure how should I capture abc#abc.com and return this in the response using regular expressions.
In WireMock the regular expressions can be used to recognize the email format in the Request Matching. For the purpose of this example I used a very crude example. Your production implementation may require a more robust approach.
This request:
http://localhost:8181/api/account/someone#somewhere.net
Is matched by this rule:
{
"request": {
"method": "GET",
"urlPathPattern": "/api/account/([a-z]*#[a-z]*.[a-z]*)"
},
"response": {
"status": 200,
"jsonBody": {
"account": "{{request.path.[2]}}"
},
"transformers": ["response-template"],
"headers": {
"Content-Type": "application/json"
}
}
}
And returns this response:
{
"account": "someone#somewhere.net"
}
It makes use of a Response Template processing functionality in WireMock. The Request Model variables [{{request.path.[2]}}] can be used to obtain sections from the request.
The same can be done using WireMock.Net - Response-Templating
The rule looks like:
{
"Request": {
"Path": {
"Matchers": [
{
"Name": "RegexMatcher",
"Pattern": "^/api/account/([a-z]*#[a-z]*.[a-z]*)$"
}
]
},
"Methods": [
"get"
]
},
"Response": {
"StatusCode": 200,
"BodyAsJson": {
"account": "{{request.PathSegments.[2]}}"
},
"UseTransformer": true,
"Headers": {
"Content-Type": "application/json"
}
}
}