我知道一个非常通用的标题,我的问题是我在lua编码而我正在尝试做一个string.gsub,但它似乎没有找到我的模式。下面找到所有细节。任何帮助表示赞赏。
消息变量包含:
{
"started_at":1456170948017,
"response":{
"status":0,
"size":"0",
"headers":{
"connection":"keep-alive"
}
},
"authenticated_entity":{
"consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
"id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
},
"request":{
"method":"POST",
"uri":"\/SecureApi\/Logon",
"size":"565",
"request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
"querystring":{
},
"headers":{
"x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
"accept-language":"en-US,en;q=0.8",
"content-type":"application\/json",
"connection":"keep-alive",
"content-length":"78",
"x-consumer-username":"kylec",
"cache-control":"no-cache",
"host":"192.168.10.122:8000",
"cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
"accept-encoding":"gzip, deflate",
"origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
"accept":"*\/*",
"apikey":"test",
"postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
"user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
}
},
"client_ip":"192.168.10.143",
"api":{
"upstream_url":"https:\/\/stage.site.com",
"request_path":"\/SecureApi",
"id":"90ba1710-1e50-4070-95d0-deb76d33e312",
"created_at":1454625397000,
"name":"secure_account_stage",
"request_host":"stage.site.com"
},
"latencies":{
"request":0,
"kong":0,
"proxy":-1
}
}
代码:
string.gsub(msg, '"querystring":{}', '"querystring":{'..BuildURLParameters()..'}')
string.gsub(msg, '},"headers":{','},"body":{'..BuildBodyParameters()..'},"headers":{')
预期结果:
{
"started_at":1456170948017,
"response":{
"status":0,
"size":"0",
"headers":{
"connection":"keep-alive"
}
},
"authenticated_entity":{
"consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
"id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
},
"request":{
"method":"POST",
"uri":"\/SecureApi\/Logon",
"size":"565",
"request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
"querystring":{ somejsoninhere },
"body":{ somejsoninhere },
"headers":{
"x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
"accept-language":"en-US,en;q=0.8",
"content-type":"application\/json",
"connection":"keep-alive",
"content-length":"78",
"x-consumer-username":"kylec",
"cache-control":"no-cache",
"host":"192.168.10.122:8000",
"cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
"accept-encoding":"gzip, deflate",
"origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
"accept":"*\/*",
"apikey":"test",
"postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
"user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
}
},
"client_ip":"192.168.10.143",
"api":{
"upstream_url":"https:\/\/stage.site.com",
"request_path":"\/SecureApi",
"id":"90ba1710-1e50-4070-95d0-deb76d33e312",
"created_at":1454625397000,
"name":"secure_account_stage",
"request_host":"stage.site.com"
},
"latencies":{
"request":0,
"kong":0,
"proxy":-1
}
}
结果:
{
"started_at":1456170948017,
"response":{
"status":0,
"size":"0",
"headers":{
"connection":"keep-alive"
}
},
"authenticated_entity":{
"consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
"id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
},
"request":{
"method":"POST",
"uri":"\/SecureApi\/Logon",
"size":"565",
"request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
"querystring":{
},
"headers":{
"x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
"accept-language":"en-US,en;q=0.8",
"content-type":"application\/json",
"connection":"keep-alive",
"content-length":"78",
"x-consumer-username":"kylec",
"cache-control":"no-cache",
"host":"192.168.10.122:8000",
"cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
"accept-encoding":"gzip, deflate",
"origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
"accept":"*\/*",
"apikey":"test",
"postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
"user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
}
},
"client_ip":"192.168.10.143",
"api":{
"upstream_url":"https:\/\/stage.site.com",
"request_path":"\/SecureApi",
"id":"90ba1710-1e50-4070-95d0-deb76d33e312",
"created_at":1454625397000,
"name":"secure_account_stage",
"request_host":"stage.site.com"
},
"latencies":{
"request":0,
"kong":0,
"proxy":-1
}
}
答案 0 :(得分:0)
正如 @EinsteinK 指出的那样,您正在查询字符串文字,但不会考虑空格。
使其正常运行的最简单方法正是他所建议的:使用[["querystring":{%s*}]]
。
为了省略冗余写作" querystring",你可以写:
string.gsub(msg, '("querystring"):{%s*}', '%1:{'..BuildURLParameters()..'}')
括号将表达式中与表达式相匹配的任何内容放入capture
中,%n
插入第n个捕获。
你可以使用捕获来缩进它,但如果不考虑相当的输出,你就不应该为此烦恼。
<强>输出:强>
{
"started_at":1456170948017,
"response":{
"status":0,
"size":"0",
"headers":{
"connection":"keep-alive"
}
},
"authenticated_entity":{
"consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
"id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
},
"request":{
"method":"POST",
"uri":"\/SecureApi\/Logon",
"size":"565",
"request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
"querystring":{somejson},
"headers":{
"x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
"accept-language":"en-US,en;q=0.8",
"content-type":"application\/json",
"connection":"keep-alive",
"content-length":"78",
"x-consumer-username":"kylec",
"cache-control":"no-cache",
"host":"192.168.10.122:8000",
"cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
"accept-encoding":"gzip, deflate",
"origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
"accept":"*\/*",
"apikey":"test",
"postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
"user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
}
},
"client_ip":"192.168.10.143",
"api":{
"upstream_url":"https:\/\/stage.site.com",
"request_path":"\/SecureApi",
"id":"90ba1710-1e50-4070-95d0-deb76d33e312",
"created_at":1454625397000,
"name":"secure_account_stage",
"request_host":"stage.site.com"
},
"latencies":{
"request":0,
"kong":0,
"proxy":-1
}
} 1