【发布时间】:2016-02-23 16:23:43
【问题描述】:
我知道一个非常通用的标题,我的问题是我在 lua 中编码并且我正在尝试做一个 string.gsub 但它似乎没有找到我的模式。在下面找到所有详细信息。任何帮助表示赞赏。
Msg 变量包含:
{
"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
}
}
【问题讨论】:
-
首先:最好只是JSON解码,添加数据并再次JSON编码。其次: string.gsub 不会像您想象的那样忽略空格(如换行符)。例如
'"querystring":{}'应该是'"querystring":{%s*}'以使其也计算换行符。 -
@EinsteinK 好的,我明白了。尽管如果数据不是 JSON 格式,我必须查看数据的结构(表格等)。天真地,这些数据来自 nginx。