harmony 鸿蒙@system.fetch (Data Request)

2022-08-09 浏览 (1124)

@system.fetch (Data Request)

NOTE

  • The APIs of this module are no longer maintained since API version 6. You are advised to use @ohos.net.http instead.

  • The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import fetch from '@system.fetch';

fetch.fetch3+

fetch(options:{
  url: string;
  data?: string|object;
  header?: Object;
  method?: string;
  responseType?: string;
  success?: (data: FetchResponse) => void;
  fail?: (data: any, code: number) => void;
  complete?: () => void;
} ): void

Obtains data through a network.

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
urlstringYesResource URL.
datastring |ObjectNoRequest parameter, which can be a string or a JSON object. For details, see the mapping between data and Content-Type.
headerObjectNoRequest header.
methodstringNoRequest method. The default value is GET. The value can be OPTIONS, GET, HEAD, POST, PUT, **DELETE **or TRACE.
responseTypestringNoResponse type. The return type can be text or JSON. By default, the return type is determined based on Content-Type in the header returned by the server. For details, see return values in the success callback.
successFunctionNoCalled when the API call is successful. The return value is defined by FetchResponse.
failFunctionNoCalled when API call has failed.
completeFunctionNoCalled when the API call is complete.

Table 1 Mapping between data and Content-Type

dataContent-TypeDescription
stringNot setThe default value of Content-Type is text/plain, and the value of data is used as the request body.
stringAny typeThe value of data is used as the request body.
ObjectNot setThe default value of Content-Type is application/x-www-form-urlencoded. The data value is encoded based on the URL rule and appended in the request body.
Objectapplication/x-www-form-urlencodedThe value of data is encoded based on the URL rule and is used as the request body.

FetchResponse3+

System capability: SystemCapability.Communication.NetStack

NameTypeReadableWritableDescription
codenumberYesNoServer status code.
datastring |ObjectYesNoThe type of the returned data is determined by responseType. For details, see the mapping between responseType and data in success callback.
headersObjectYesNoAll headers in the response from the server.

Table 2 Mapping between responseType and data in success callback

responseTypedataDescription
N/AstringWhen the type in the header returned by the server is text/*, application/json, application/javascript, or application/xml, the value is the text content.
textstringText content.
jsonObjectA JSON object.

Example

export default {
  data: {
    responseData: 'NA',
    url: "test_url",
  },
  fetch: function () {
    var that = this;
    fetch.fetch({
      url: that.url,
      success: function(response) {
        console.info("fetch success");
        that.responseData = JSON.stringify(response);
      },
      fail: function() {
        console.info("fetch fail");
      }
    });
  }
}

NOTE HTTPS is supported by default. To support HTTP, you need to add "network" to the config.json file, and set the attribute "cleartextTraffic" to true. That is:

{
  "deviceConfig": {
    "default": {
      "network": {
        "cleartextTraffic": true
      }
      ...
    }
  }
  ...
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/9d669a9237794491a0620e6637f067df