鸿蒙开发之HTTP请求

发布时间 2023-12-20 19:36:12作者: 谢玉胜

1.  申请网络权限

在module.json文件中添加

"module": {
  "reqPermissions": [
      {"name": "ohos.permission.INTERNET"}  
   ]      
}

 2. 发起

// 导入http模块
import http from '@ohos.net.http';
//创建http 实例
const httpRequest = http.createHttp();

// 发起请求

let url = ""
let _request = httpRequest.request(url, {

   method: http.RequestMethod.GET,
    //POST 请求请求参数
   extarDate: {
     "param1": "value1",
      "param2": "value2"  
  }  ,
  header: {
      'Content-Type': 'application/json'
    }
}) 

 最后promise拿到结果