HarmonyOS http网络访问,参数问题

我这边使用http网络请求是post方式,参数是以json的方式放到body里面,这种方式应该怎么怎么写?

HarmonyOS
2024-11-08 09:09:48
1063浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

示例如下:

//public.ts 
export default class  Response { 
  /** 
   * 响应码 
   */ 
  code:number 
  /** 
   * 响应消息 
   */ 
  message:string 
  /** 
   * 响应数据 
   */ 
  data:any 
} 
 
//http_request.ts 导入请求  
import http from '@ohos.net.http'; 
//响应格式 
import Response from './public' 
//导出去一个请求函数 使用axios风格请求数据、参数可自行增加 
export function request(url:string,method: http.RequestMethod,data?:any): Promise<Response> { 
  const BASE_URL =  "url" 
  // const BASE_URL =  "http://api.common.smallfeiyu.cn" 
  let httpRequest = http.createHttp(); 
  let responseResult = httpRequest.request( BASE_URL+ url,{ 
    method: method, 
    //请求头设置 
    header: { 
      'Content-Type': 'application/x-www-form-urlencoded' 
    }, 
    //携带额外参数 
    extraData: JSON.stringify(data), 
  }); 
 
  let response = new Response(); 
  // 处理数据,并返回 
  return responseResult.then((result: http.HttpResponse) => { 
    if (result.responseCode === 200) { 
      let res: Response = JSON.parse(`${result.result}`); 
      response.data = res.data; 
      response.code = res.code; 
      response.message = res.message; 
    } else { 
      response.message = '请求错误'; 
      response.code = 400; 
    } 
    return response; 
  }).catch((error) => { 
    response.message = '请求错误'; 
    response.code = 400; 
    return response; 
  }); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
分享
微博
QQ
微信
回复
2024-11-08 17:15:27


相关问题
HarmonyOS http 请求 post 参数问题
2424浏览 • 1回复 待解决
鸿蒙应用进行http本地网络访问
4416浏览 • 1回复 待解决
关于JS http请求参数的传递问题
8431浏览 • 2回复 待解决
HarmonyOS http+ip访问不了
705浏览 • 1回复 待解决
HarmonyOS http post请求参数传递
1137浏览 • 1回复 待解决
preview能否访问网络
763浏览 • 1回复 待解决
HarmonyOS 网络请求参数转换
719浏览 • 1回复 待解决
HarmonyOS http网络请求封装的Demo
918浏览 • 1回复 待解决
使用http请求网络地址
1730浏览 • 1回复 待解决
HTTP GET请求时如何传递参数
4173浏览 • 1回复 待解决
HarmonyOS HTTP-post请求接收不到参数
1065浏览 • 1回复 待解决
不能访问http://repo.ark.tools.huawei.com
9131浏览 • 1回复 待解决
Web组件访问本地资源并传递参数
1409浏览 • 1回复 待解决
HarmonyOS http网络库的开源代码在哪
742浏览 • 1回复 待解决
Stage模型中如何申请网络访问权限
3027浏览 • 1回复 待解决
HarmonyOS Http请求头问题咨询
1151浏览 • 1回复 待解决
HarmonyOS page 参数问题
793浏览 • 1回复 待解决