簡體   English   中英

nodejs發送數據到api axios

[英]nodejs send data to api axios

下面是我的nodejs配置:

 const Tail = require('tail').Tail; const axios = require("axios"); console.log("Watching..."); var tail = new Tail("./nxerror.log"); tail.watch() tail.on("line", data => { console.log("data changeed"); console.log(data); var username = 'Eve' var password = 'changeme' const body = {"action":"EventsRouter", "method":"add_event", "data": [{"summary":"test","device":"test","message":"msg","component":"testhost","severity":"5","evclasskey":"nxlog","evclass":"/nxlog/perf","monitor":"localhost"}],"type":"rpc","tid":2} var url = 'https://myurl.com/zport/dmd/evconsole_router'; axios({ method:'post', url:url, headers: { 'Content-Type': 'application/json' }, data: body, auth: { username: username, password: password } }).then(function (response) { console.log("inside then"); console.log(response); }).catch(function (error) { console.log("inside catch"); console.log(error); }); console.log("after axios"); })

當在特定日志文件中創建條目時,我嘗試將定義的 json 數據發送到 api url。 我的控制台沒有顯示錯誤,但是數據也到達了 api。

這是我的日志:

> 0|server   |
> {"@version":"1","host":"strproelk03","message":"60.16.48.61 - -
> [12/May/2020:23:24:06 -0600] \\\"GET /robots.txt HTTP/1.1\\ 404 443
> 20","ApplicationName":"Oasis","Severity":"ERROR","@timestamp":"2020-05-16T20:10:39.632Z","tagfile_path":"log.log"}
> 0|server   | after axios 
> 0|server   | Watching...

在 axios 是我看到的最后一條消息之后,它在我的代碼的“內部然后”或“內部緩存”部分中都沒有 go。 為什么進不去?

編輯:我添加了超時並繞過了我的證書驗證,如下所示:

 axios.defaults.timeout = 1000; const http = require('http'); const https = require('https'); const httpAgent = new http.Agent({ rejectUnauthorized: false, }); const httpsAgent = new https.Agent({ rejectUnauthorized: false, }); axios({ method:'post', url:url, httpsAgent: httpsAgent, httpAgent: httpAgent, headers: {'Content-Type': 'application/json' }, auth: { username: username, password: password }

這仍然不會改變我的最終結果。 仍然沒有錯誤也沒有數據傳遞。

然而,url 是有效的,因為我測試了直接使用 curl 命令發送數據,這些命令沒有任何問題。

GET /robots.txt 不存在。

您有一個Content-Type: application/json您的正文請求需要作為JSON.stringify(json)發送

const Tail = require('tail').Tail;
const axios = require("axios");

exports.myFunction = function myFunction () {    

    console.log("Watching...");
    var tail = new Tail("./nxerror.log");
    tail.watch()
    tail.on("line", data => {
    console.log("data changeed");
      console.log(data);
    var username = 'Eve'
    var password = 'changeme'

    const body = {"action":"EventsRouter", "method":"add_event", "data": [{"summary":"test","device":"test","message":"msg","component":"testhost","severity":"5","evclasskey":"nxlog","evclass":"/nxlog/perf","monitor":"localhost"}],"type":"rpc","tid":2}

    var url = 'https://myurl.com/zport/dmd/evconsole_router';
    var api = axios.create();

   return api.request({
            method:'post',
            url:url,
            headers: { 'Content-Type': 'application/json' },
            data: JSON.stringify(body),
            auth: {
                username: username,
                password: password
            }
        }).then(function (response) {
    console.log("inside then");
        console.log(response);
      })
      .catch(function (error) {
    console.log("inside catch");
        console.log(error);
      });
    console.log("after axios");
    })
}

驗證它實際上很簡單; 您只需將現有的 URL 替換為如下所示的測試端點: https://postman-echo.com/post ,然后您可以看到您收到錯誤或收到有效響應。 我的假設是您的請求無法到達服務器,並且由於未設置 Axios 的超時,因此 Axios 只是在等待。 您是否嘗試過通過 postman 訪問端點?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM