解析AWS-Clite-gateway-api响应问题

时间:2019-03-13 00:39:09

标签: json rest aws-sdk aws-api-gateway

这是我的JSON响应。我想解析以下JSON,并从header.location获取Site_id。我可以获取状态,消息值,但不能获取位置值。

实际上是响应标头,我正在尝试解析。

请,有人可以在这里提供帮助吗?

MAX_WAIT_SECONDS

这里是输出:但是我只需要Status,StatusText,标题下的位置和数据(@底部)作为输出。我试图解析上面代码中的输出,但不走​​运。 任何人都可以帮助解析响应。

    var apigClientFactory = require('aws-api-gateway-client').default;
let awsbody = JSON.parse(process.argv[2].split('\\').join('') || '{}');

    var apigClient = apigClientFactory.newClient({
    invokeUrl: awsbody.endPoint, // REQUIRED
    accessKey: awsbody.awsAccessKey, // REQUIRED
    secretKey: awsbody.awsSecreteKey, // REQUIRED
    region: awsbody.awsRegion, // REQUIRED: The region where the AapiKeyloyed.
    retryCondition: (err) => { // OPTIONAL: Callback to further control if request should be retried.  Uses axon-

retry plugin.
        return err.response && err.response.status === 500;
    }
});

var pathParams = awsbody.awsPathParams;
// Template syntax follows url-template https://www.npmjs.com/package/url-template
var pathTemplate = awsbody.awsPathTemplate; // '/api/v1/sites'
var method = awsbody.method; // 'POST';
var additionalParams = awsbody.awsAdditionalParams; //queryParams & Headers if any
//var additionalParams = {headers: { "x-apigw-api-id": 'vs2i50xvo4'}};

var body = awsbody.requestBody;
var output = {};


apigClient.invokeApi(pathParams, pathTemplate, method, additionalParams, body)
    .then(function(result) {
console.log(result)

    }).catch(function(result) {
             console.log(result)
    });

1 个答案:

答案 0 :(得分:1)

实际结果不是json格式的变量,请查看我的有效Json格式的代码。

您现在可以根据需要获取位置。

let v = '{"data": {"status": "sucess","message": "request has been accepted"},"responsecode": 202,"responsetext": "Accepted","headers": {"server": "Server","date": "Tue, 12 Mar 2019 20:34:36 GMT","content-type": "application/json","content-length": "61","x-amzn-requestid": "400377cd-4506-11e9-b941-3984677a719c","x-amz-apigw-id": "WciqVH8pPHcFgWg=","location": "/transit-connectivity/api/v1/jobs/8125d985-3d90-473d-b799-c9974","x-amzn-trace-id": "Root=1-5c8817db-5522b10bf2068ac2509ae35b;Sampled=0","connection": "close"}}';

let value = JSON.parse(v); 
console.log("location :"value.headers.location);

控制台:

  location :  /transit-connectivity/api/v1/jobs/8125d985-3d90-473d-b799-c9974