简体   繁体   English

使用Node.js中的不同方法发出多个Https请求

[英]Making multiple Https requests with different methods in Node.js

I want to make a POST request that returns json data and then i want to get some keys values and then create a json string and then PUT that json sting to a url. 我想发出一个返回json数据的POST请求,然后我想获取一些键值,然后创建一个json字符串,然后将json插入网址。 And ideally would like this to happen every 30s. 理想情况下,这希望每30秒发生一次。 I have some code set up here https://runkit.com/lukejamison/5d2dfbf99644eb0013c64a56 我在这里设置了一些代码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());
        }
    });
}

Considering you use-case, Perform PUT request synchronously/asynchronously after the successful response from the POST request. 考虑到您的用例,在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));                 
        })    
        */

    })    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM