99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

Moralis http請(qǐng)求

2022-05-12 10:43 更新

Moralis.Cloud.httpRequest

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

一個(gè)簡(jiǎn)單的 GET 請(qǐng)求如下所示:

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? 返回一個(gè)promise,該promise將在成功的 HTTP 狀態(tài)碼上得到解決; 否則,promise 將被拒絕。 在上面的例子中,我們使用 then() 來(lái)處理這兩個(gè)結(jié)果。

指定端口號(hào)的 GET 請(qǐng)求如下所示:

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);
});

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

默認(rèn)情況下,?Moralis.Cloud.httpRequest? 不遵循由 HTTP 3xx 響應(yīng)代碼引起的重定向,可以使用 ?followRedirects: true? 選項(xiàng)。

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ù)

您可以通過(guò)在選項(xiàng)對(duì)象上設(shè)置參數(shù)來(lái)指定要附加到 URL 末尾的查詢參數(shù)。 您可以傳遞鍵值對(duì)的 JSON 對(duì)象,例如:

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);
});

設(shè)置標(biāo)題

您可以通過(guò)設(shè)置選項(xiàng)對(duì)象的標(biāo)頭屬性來(lái)發(fā)送 HTTP ?header?。 假設(shè)你想設(shè)置請(qǐng)求的 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 請(qǐng)求

您可以通過(guò)設(shè)置選項(xiàng)對(duì)象的方法屬性來(lái)發(fā)送發(fā)布請(qǐng)求。 可以使用 body 設(shè)置 POST 的正文。 一個(gè)簡(jiǎn)單的例子是:

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ā)送一個(gè)帶有正文的帖子,該正文是 URL 表單編碼的正文屬性。 如果您希望對(duì)正文進(jìn)行 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 請(qǐng)求正文編碼正確,請(qǐng)始終在您的內(nèi)容類型標(biāo)頭中包含字符集。

后重定向

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

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);
});

響應(yīng)對(duì)象

傳遞給?success?和?error?的響應(yīng)對(duì)象將包含:

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


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)