我將使用 AWS 提供的 API Gateway 和 Lambda 創建一個簡單的加密服務器。
API Gateway 遠程辦公了 HTTP API 方法,支持 Payload 2.0 版本的 Payload。
此有效負載的示例如下。
{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/my/path",
"rawQueryString": "parameter1=value1¶meter1=value2¶meter2=value",
"cookies": [
"cookie1",
"cookie2"
],
"headers": {
"header1": "value1",
"header2": "value1,value2"
},
"queryStringParameters": {
"parameter1": "value1,value2",
"parameter2": "value"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authentication": {
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"authorizer": {
"jwt": {
"claims": {
"claim1": "value1",
"claim2": "value2"
},
"scopes": [
"scope1",
"scope2"
]
}
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/my/path",
"protocol": "HTTP/1.1",
"sourceIp": "IP",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "12/Mar/2020:19:03:58 0000",
"timeEpoch": 1583348638390
},
"body": "Hello from Lambda",
"pathParameters": {
"parameter1": "value1"
},
"isBase64Encoded": false,
"stageVariables": {
"stageVariable1": "value1",
"stageVariable2": "value2"
}
}
在這個 Payload 中,我想用 json 決議和利用的資料是一個主體值。
Lambda 本身提供的測驗沒有任何問題,但是當 postman 在 application/json 方法中發送請求時,body 以無法決議為 json 的字串格式包含在有效負載中。
一個例子如下。
"[\r\n {\r\n \"key\" : \"aaa\",\r\n \"value\" : \"value1\"\r\n },\r\n {\r\n \"key\" : \"bbb\",\r\n \"value\" : \"value2\"\r\n }\r\n]"
原來的body值是這樣的。
[
{
"key" : "aaa",
"value" : "value1"
},
{
"key" : "bbb",
"value" : "value"
}
]
我將在決議為 json 時使用的語言是 JavaScript。
我應該怎么做才能將此“Stringify Data”決議為 JSON?
uj5u.com熱心網友回復:
您的字串可以使用JSON.parse()轉換為 JSON
var data = "[\r\n {\r\n \"key\" : \"aaa\",\r\n \"value\" : \"value1\"\r\n },\r\n {\r\n \"key\" : \"bbb\",\r\n \"value\" : \"value2\"\r\n }\r\n]"
console.log(JSON.parse(data))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381397.html
上一篇:不使用Math.ceil()javascript舍入數字
下一篇:使用陣列陣列中的子項創建嵌套物件
