0

我想发出一个返回 json 数据的 POST 请求,然后我想获取一些键值,然后创建一个 json 字符串,然后将该 json 字符串放入 url。理想情况下,希望这种情况每 30 秒发生一次。我在这里设置了一些代码https://runkit.com/lukejamison/5d2dfbf99644eb0013c64a56

var GeoJSON = require("geojson");
const stringify = require("json-stringify-pretty-compact");
var apikey = process.env.xapikey;

var options = {
    method: 'POST',
    url: 'https://app.detrack.com/api/v1/vehicles/view/all.json',
    headers: {
        'x-api-key': apikey,
    }
};

exports.endpoint = function(httpRequest, response) {
    if (!apikey){
        return response.end('"xapikey" key is missing.');
    }
    request(options, function (error, httpResponse, body){
        try{
            var jsonresponse = JSON.parse(body)
            var lat1 = jsonresponse.vehicles[0].lat
            var lng1 = jsonresponse.vehicles[0].lng
            response.end(stringify({ geometry: { type: "Point", coordinates: [lng1, lat1]}, type: "Feature", properties:{}}));
        }catch(ex){
            response.end(ex.toString());
        }
    });
}

4

1 回答 1

0

考虑到您的用例,在 POST 请求成功响应后同步/异步执行 PUT 请求。

var request = require('request');
var GeoJSON = require("geojson");
const stringify = require("json-stringify-pretty-compact");
var apikey = process.env.xapikey;

//To create vehicle
function createVehicle(callback) {
    var options = {
        method: 'POST',
        url: 'https://app.detrack.com/api/v1/vehicles/view/all.json',
        headers: {
            'x-api-key': apikey,
        }
    };

    request(options, function (error, httpResponse, body){
        if(error) return callback(error);
        try{
            var jsonresponse = JSON.parse(body)
            var lat1 = jsonresponse.vehicles[0].lat
            var lng1 = jsonresponse.vehicles[0].lng
            const result = { geometry: { type: "Point", coordinates: [lng1, lat1]}, type: "Feature", properties:{}};
            callback(null, result);         
        }catch(ex){
            callback(ex)
        }
    }
}

//To update vehicle
function updateVehicle(data, callback) {
    var options = {
        method: 'PUT',
        url: '',
        headers: {
            'x-api-key': apikey,
        },
        body: data
    };  
    request(options, function (error, httpResponse, body){
        if(error) return callback(error);
        try{
            callback(null, body);           
        }catch(ex){
            callback(ex)
        }
    }
}


exports.endpoint = function(httpRequest, response) {
    if (!apikey){
        return response.end('"xapikey" key is missing.');
    }
    //To Create Vehicle
    createVehicle((error, createVehicleResponse) => {
        if(error) {
            response.end(error.message);
        }

        //To Update Vehicle synchronously, enpoint will return response once updateVehicle call responded back
        updateVehicle(createVehicleResponse, (error, updateVehicleResponse) => {
            if(error) {
                response.end(error.message);
            }
            response.end(stringify(createVehicleResponse));         
        }); 

        /*
        In order to exectue asynchronously, use the following snippet, which will retrun response after the createVehicle call the call updateVehicle in background
        createVehicle((error, createVehicleResponse) => {
            if(error) {
                response.end(error.message);
            }           
            updateVehicle(createVehicleResponse, (error, updateVehicleResponse) => {}); 
            response.end(stringify(createVehicleResponse));                 
        })    
        */

    })    
}
于 2019-07-21T08:07:01.540 回答