簡體   English   中英

從節點調用外部rest api而不對querystring參數進行編碼

[英]calling external rest api from node without encoding querystring params

我正在嘗試通過使用request節點模塊從節點服務器調用外部rest API

let request = require('request');

var options = { 
  method: 'POST',
  url: 'https://somerestURI:3000',
  qs: { msg: 'some|data|for|other|server' } 
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

如果我嘗試運行上面的代碼,則查詢字符串值被編碼為

some%7cdata%7cfor%7cother%7cserver

結果我沒有收到正確的答復。

但是,如果我在POSTMAN發出相同的請求。 我收到預期的輸出(我認為郵遞員未對查詢字符串進行編碼)。

所以我想要的是不對查詢字符串值進行編碼。

任何幫助將不勝感激。

作為回答在這里 ,您可以禁用encodingqsStringifyOptions

var options = { 
   method: 'POST',
   url: 'https://somerestURI:3000',
   qs: { msg: 'some|data|for|other|server' },
   qsStringifyOptions: {
      encoding: false
   } 
};

您可以使用node-rest-client軟件包。 它允許連接到任何REST API並以javascript對象的形式獲取結果。

var HttpClient = require('node-rest-client').Client;
var httpClient = new HttpClient();

// GET Call
httpClient.get("http://remote.site/rest/xml/method", function (data, response) {
       // parsed response body as js object 
       console.log(data);
       // raw response 
       console.log(response);
});)

或用於POST通話

var args = {
     data: { test: "hello" },
     headers: { "Content-Type": "application/json" }
};
//POST Call
httpClient.post("http://remote.site/rest/xml/method", args, function (data, response) {
      // parsed response body as js object 
      console.log(data);
      // raw response 
      console.log(response);
});

暫無
暫無

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

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