WireMock bodyPatterns XML Match regex - regex

I am using WireMock to mock SOA service, but I have an issue with the bodyPattern XML, can I use regex inside XML?
My request header is changing based on the request time, I just want to match anything inside the header.
{
"request" : {
"url" : "/service/v1/WebService",
"method" : "POST",
"bodyPatterns" : [ {
"equalToXml" :"\\Q<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header>
<SOAP-ENV:Header>
I want to match whatever inside header.
</SOAP-ENV:Header>
} ]
},

For example your request is something like below,
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ex="http://example.com">
<soapenv:Header/>
<soapenv:Body>
<ex:somebody>
<ex:input>
<ex:name>John</ex:name>
<ex:phone>7414444</ex:phone>
<ex:role>teacher</ex:role>
</ex:input>
</ex:somebody>
</soapenv:Body>
then your JSON file could be,
{
"request" : {
"url" : "/service/v1/WebService",
"bodyPatterns" : [ {
"matchesXPath": "//ex:input[ex:name=\"John\" and xw:phone=\"7414444\"]",
"xPathNamespaces" : {
"ex" : "http://example.com"
}
}]
},
"response" : {
"status" : 200,
"headers": {
"Content-Type": "text/xml;charset=UTF-8"
},
"body" : "<Abody>"
}}

You can use Xpath to arrive at same result
"bodyPatterns": [
{
"matchesXPath": "your xpath"
}

Related

Trying to remove item from a list in AWS AppSync in VTL

I am trying to make a custom resolver in VTL that will remove a certain string from a dynamodb list. I was trying to use $util.list.copyAndRemoveAll to remove the friend from the list. But I am not quite sure how to use it and the documentation isn't very clear to me and I don't even know if I am using the function correctly within VTL. https://docs.aws.amazon.com/appsync/latest/devguide/list-helpers-in-util-list.html
Thank you in advance.
GraphQL mutation
type Mutation {
removeFromIncomingFriendList(pk: String!, sk: String!, friend: String!): String!
}
#user_incoming_friend_requests is a list of usernames of users that have sent friend requests.
{
"version" : "2018-05-29",
"operation" : "UpdateItem",
"key" : {
"pk" : { "S" : "USER#USERNAME" },
"sk" : { "S" : "METADATA#USERNAME" },
},
"update": {
"expression" : "SET #user_incoming_friend_requests = :newList ADD version :plusOne",
"expressionNames": {
"#user_incoming_friend_requests" : "user_incoming_friend_requests",
},
"expressionValues" : {
":newList": $util.list.copyAndRemoveAll("#user_incoming_friend_requests", [${context.arguments.friend}])
":plusOne" : {"N":1}
}
}
}

AWS API gateway and elastic search get query

I need to call elastic search engine directly from api gateway using http connection eg.
https:////_doc/_search?pretty&filter_path=hits.hits._source
i have n number of orders in elastic search engine which i want to get using get query ,but i want only array of json i posted and dont want any other information in the response.how can i do that ?
eg.
this is what i am getting :
{
"hits" : {
"hits" : [
{
"_index" : "gpss_orders",
"_type" : "_doc",
"_id" : "4867254",
"_score" : 1.0,
"_source" : {
"orderId" : 4867254,
"loadId" : 18214,
"orderTypeId" : 1
}
]
}
}
But i would want response something like this :
[ {
"orderId" : 4867254,
"loadId" : 18214,
"orderTypeId" : 1
}]
do i need to change in api gateway method response?
i changed the api gateway method response template and got the expected out
#set($esOutput = $input.path('$.hits.hits'))
#set($orders = [])
#foreach( $elem in $esOutput )
#set($order = $elem["_source"])
#set($response = $orders.add($order) )
#end
$orders
but now the problem i am facing is that though response from elastic search engine is the proper json,response after method integration template update become like this without any braces :
[{orderId=4867254, loadId=18214, orderTypeId=1, orderTypeName=Fuel}]
response from elastic search :
"took" : 1,
"hits" : {
"hits" : [
{
"_id" : "4867254",
"_score" : 1.0,
"_source" : {
"orderId" : 4867254,
"loadId" : 18214,
"orderTypeId" : 1,
There isn't a way to shape the return object from elasticsearch. Depending on how you access this data, you could have your own server-side code as a proxy make the query and remove extraneous information before returning it to the clients. A bonus is you can use the proxy to decide what information to return depending on factors such as permissions, caching or rate-limiting, etc.

How I can update boolean field in DynamoDb with Mapping template?

I have next code for update item in DB with mapping template:
$!{expSet.put("available", ":available")}
$!{expValues.put(":available", { "BOOL": $ctx.args.available })}
when I send available = false - it's ok, but if available = true I get error
"Unable to parse the JSON document: 'Unexpected character (':' (code
58)): was expecting double-quote to start field name
schema in GraphQl
type Item {
....
available: Boolean!
....
}
What I do wrong ?
Your UpdateItem request mapping template should look something like:
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"id" : { "S" : "${context.arguments.id}" }
},
"update" : {
"expression" : "SET #available = :available",
"expressionNames": {
"#available" : "available"
},
"expressionValues": {
":available" : { "BOOL": ${context.arguments.available} }
}
}
}

How to split json value in log file using grok/regular expression

I have one log file which I need to extract the json content from the file and I need to parse it using logstash json filter. I wrote one grok pattern but which it is not working properly. Below is my log file.
2016-12-18 12:13:52.313 -08:00 [Information] 636176600323139749 1b2c4c40-3da6-46ff-b93f-0eb07a57f2a3 18 - API: GET https://aaa.com/o/v/S?$filter=uid eq '9'&$expand=org($filter=org eq '0')
{
"Id": "1b",
"App": "D",
"User": "",
"Machine": "DC",
"RequestIpAddress": "xx.xxx.xxx",
"RequestHeaders": {
"Cache-Control": "no-transform",
"Connection": "close",
"Accept": "application/json"
},
"RequestTimestamp": "2016-12-18T12:13:52.2609587-08:00",
"ResponseContentType": "application/json",
"ResponseContentBody": {
"#od","value":[
{
"uid":"","sId":"10,org":[
{
"startDate":"2015-02-27T08:00:00Z","Code":"0","emailId":"xx#gg.COM"
}
]
}
]
},
"ResponseStatusCode": 200,
"ResponseHeaders": {
"Content-Type": "application/json;"
},
"ResponseTimestamp": "2016-12-18T12:13:52.3119655-08:00"
}
My Grok pattern
grok {
match => [ "message","%{TIMESTAMP_ISO8601:exclude}%{GREEDYDATA:exclude1}(?<exclude2>[\s])(?<json_value>[\W\w]+)"]
}
Assuming this is all one message (it's not multiline, or has been combined before now) and there's a space between the URI and the json, this grok pattern should work:
%{TIMESTAMP_ISO8601} %{NOTSPACE:timezone} \[%{WORD:severity}] %{WORD:field1} %{UUID:field2} %{NUMBER:field3} - API: %{WORD:verb} (?<field4>[^\{]*) %{GREEDYDATA:json}
It would have been nice to use %{URI}, but the string you have is not a valid URI (it contains unescaped spaces).

How to get entire request headers/querystrings in Serverless framework?

I started to try Serverless framework, but it looks little confusable for some points...
One of them is request headers/querystrings,
I made request template like this:
s-templates.json
{
"apiRequestTemplate": {
"application/json": {
"httpMethod": "$context.httpMethod",
"body": "$input.json('$')",
"queryParams" : "$input.params().querystring",
"headerParams" : "$input.params().header",
"headerParamNames" : "$input.params().header.keySet()",
"contentTypeValue" : "$input.params().header.get('Content-Type')"
}
}
}
s-function.json
"requestParameters": {},
"requestTemplates": "$${apiRequestTemplate}",
With this setting, I expected to get the request something like this:
{
"body" : {}
"contentTypeValue" : ""
"headerParamNames" : ["Accept", "Accept-Encoding", ... ],
"headerParams" : {
"Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding" : "gzip, deflate, sdch, br, Accept-Language=ja,en-US;q=0.8,en;q=0.6",
...
},
"httpMethod" : "GET",
"queryParams" : {
"category" : "Some Category"
}
}
But in real, what I get is:
{
"body" : {}
"contentTypeValue" : ""
"headerParamNames" : "[Accept,Accept-Encoding, ... ]",
"headerParams" : "{Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", Accept-Encoding=gzip, deflate, sdch, br, Accept-Language=ja,en-US;q=0.8,en;q=0.6", ...}",
"httpMethod" : "GET",
"queryParams" : "{category=Some Category}"
}
This results inconvenient to handle.
I know also, method like below:
s-function.json
"requestParameters": {},
"requestTemplates": {
"application/json": "{\"category\":\"$input.params('category')\"}"
},
But this is also inconvenient need to specify all parameters in configuration..
Is there any way to get entire request-headers / query-strings as json object in lambda function?
Modified after answer
I tried to change s-template.json to
"queryParams" : "$util.parseJson($input.params().querystring)",
"headerParams" : "$util.parseJson($input.params().header)",
But result was same...
And, in AWS document, what I want can be seen here: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference
#set($allParams = $input.params())
{
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
}
}
But I don't know how to set this setting to Serverless framework's s-templates.json...
I use following request template. It will wrap data, path, headers,params,query into a JSON object and pass it to the function.
{
"application/json": {
"data": "$input.json('$')",
"path": "$context.resourcePath",
"method": "$context.httpMethod",
"headers": "{#foreach($header in $input.params().header.keySet())\"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end#end}",
"params": "{#foreach($param in $input.params().path.keySet())\"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end#end}",
"query": "{#foreach($queryParam in $input.params().querystring.keySet())\"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\" #if($foreach.hasNext),#end#end}"
}
}
You can refer Apache Velocity Templates to get better understanding about the inner syntax such as #foreach($header in .....).
Have you tried $util.parseJson()? It takes the json as a string and turns it into a traditional json object.