Moralis http請求

2022-05-12 10:43 更新

Moralis.Cloud.httpRequest

?Moralis Dapp? 包括 ?Moralis.Cloud.httpRequest?。 它允許您向任何 HTTP 服務器發(fā)送 HTTP 請求。 此函數(shù)采用選項對象來配置調(diào)用。

一個簡單的 GET 請求如下所示:

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.httpRequest({
  url: 'https://www.awesomewebsite.com/'
}).then(function(httpResponse) {
  // success
  logger.info(httpResponse.text);
},function(httpResponse) {
  // error
  logger.error('Request failed with response code ' + httpResponse.status);
});

?Moralis.Cloud.httpRequest? 返回一個promise,該promise將在成功的 HTTP 狀態(tài)碼上得到解決; 否則,promise 將被拒絕。 在上面的例子中,我們使用 then() 來處理這兩個結果。

指定端口號的 GET 請求如下所示:

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.httpRequest({
  url: 'https://www.awesomewebsite.com:8080/'
}).then(function(httpResponse) {
  logger.info(httpResponse.text);
}, function(httpResponse) {
  logger.error('Request failed with response code ' + httpResponse.status);
});

有效端口號為 80、443 以及從 1025 到 65535 的所有數(shù)字。

默認情況下,?Moralis.Cloud.httpRequest? 不遵循由 HTTP 3xx 響應代碼引起的重定向,可以使用 ?followRedirects: true? 選項。

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.httpRequest({
  url: 'https://www.awesomewebsite.com/',
  followRedirects: true
}).then(function(httpResponse) {
  logger.info(httpResponse.text);
}, function(httpResponse) {
  logger.error('Request failed with response code ' + httpResponse.status);
});

查詢參數(shù)

您可以通過在選項對象上設置參數(shù)來指定要附加到 URL 末尾的查詢參數(shù)。 您可以傳遞鍵值對的 JSON 對象,例如:

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.httpRequest({
  url: 'http://www.google.com/search',
  params: {
    q : 'Sean Plott'
  }
}).then(function(httpResponse) {
  logger.info(httpResponse.text);
}, function(httpResponse) {
  logger.error('Request failed with response code ' + httpResponse.status);
});

或者,像這樣的原始String:

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.httpRequest({
  url: 'http://www.google.com/search',
  params: 'q=Sean Plott'
}).then(function(httpResponse) {
  logger.info(httpResponse.text);
}, function(httpResponse) {
  logger.error('Request failed with response code ' + httpResponse.status);
});

設置標題

您可以通過設置選項對象的標頭屬性來發(fā)送 HTTP ?header?。 假設你想設置請求的 Content-Type,你可以這樣做:

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.httpRequest({
  url: 'http://www.example.com/',
  headers: {
    'Content-Type': 'application/json;charset=utf-8'
  }
}).then(function(httpResponse) {
  logger.info(httpResponse.text);
}, function(httpResponse) {
  logger.error('Request failed with response code ' + httpResponse.status);
});

發(fā)送 POST 請求

您可以通過設置選項對象的方法屬性來發(fā)送發(fā)布請求。 可以使用 body 設置 POST 的正文。 一個簡單的例子是:

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.httpRequest({
  method: 'POST',
  url: 'http://www.example.com/create_post',
  body: {
    title: 'Vote for Pedro',
    body: 'If you vote for Pedro, your wildest dreams will come true'
  }
}).then(function(httpResponse) {
  logger.info(httpResponse.text);
}, function(httpResponse) {
  logger.error('Request failed with response code ' + httpResponse.status);
});

這將向 ?http://www.example.com/create_post? 發(fā)送一個帶有正文的帖子,該正文是 URL 表單編碼的正文屬性。 如果您希望對正文進行 JSON 編碼,則可以這樣做:

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.httpRequest({
  method: 'POST',
  url: 'http://www.example.com/create_post',
  headers: {
    'Content-Type': 'application/json;charset=utf-8'
  },
  body: {
    title: 'Vote for Pedro',
    body: 'If you vote for Pedro, your wildest dreams will come true'
  }
}).then(function(httpResponse) {
  logger.info(httpResponse.text);
}, function(httpResponse) {
  logger.error('Request failed with response code ' + httpResponse.status);
});

為確保您的 HTTP 請求正文編碼正確,請始終在您的內(nèi)容類型標頭中包含字符集。

后重定向

默認情況下,?Moralis.Cloud.httpRequest? 不遵循由 HTTP 3xx 響應代碼引起的重定向。 您可以使用 ?followRedirects ?選項更改此行為以遵循重定向:

const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.httpRequest({
  url: 'http://www.example.com/',
  followRedirects: true
}).then(function(httpResponse) {
  logger.info(httpResponse.text);
}, function(httpResponse) {
  logger.error('Request failed with response code ' + httpResponse.status);
});

響應對象

傳遞給?success?和?error?的響應對象將包含:

  • ?status ?- HTTP 響應狀態(tài)。
  • ?headers ?- 響應標頭。
  • ?buffer ?- 響應正文的原始字節(jié)表示。
  • ?text ?- 原始響應正文。
  • ?data ?- 解析的響應,如果 Cloud Code 知道如何解析發(fā)送的內(nèi)容類型。
  • ?cookies ?- 服務器發(fā)送的cookies。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號