HarmonyOS http请求如何将请求结果与interface对应

http请求如何将请求结果与interface对应,通过httpRequest.request获取到的数据如何与interface对应。

http代码:

let httpRequest = http.createHttp();
let httpResult = httpRequest.request(
    'https://api.xzyunchuang.com/hfyuser/userapi/getauthcode',
    // 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定"EXAMPLE\_URL",
    {
        method : http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
        // 开发者根据自身业务需要添加header字段
        header : {
            'Content-Type' : 'application/json',
            'Authorization' : '',
        },
        // 当使用POST请求时此字段用于传递内容
        extraData : {
            "data" : {
                phoneNum : phoneNum,
            },
        },
        expectDataType : http.HttpDataType.STRING, // 可选,指定返回数据的类型
        usingCache : true,                         // 可选,默认为true
        priority : 1,                              // 可选,默认为1
        connectTimeout : 60000,                    // 可选,默认为60000ms
        readTimeout : 60000,                       // 可选,默认为60000ms
        usingProxy : false, // 可选,默认不使用网络代理,自API 10开始支持该属性
    },
    (err, data) = > {
        if (!err) {                               // 获取网络数据成功
            console.log('Result:' + data.result); // 获取到网络的返回值
            AlertDialog.show({message : JSON.stringify(data.result)})
        } else { // 获取网络数据失败
            AlertDialog.show({message : '获取失败'}) console.log('error:' + JSON.stringify(err)); // 请求数据失败反馈
        }
    }
)
  • 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.

interface代码:

export interface getAuthCodeResponse {
  code: string;
  msg: string;
  data: GeneratedTypeLiteralInterface_1;
}
interface GeneratedTypeLiteralInterface_1 {
  cookie: string;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
HarmonyOS
2024-12-23 16:07:54
616浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可用HttpResponse接收,参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-http-V5#httpresponse

如果需要转换可以根据HttpResponse业务自行解析:

// data为http.HttpResponse
let myData: getAuthCodeResponse = {
  code: data.responseCode.toString(),
  msg: data.result.toString(),
  data: {
    cookie: data.cookies
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
分享
微博
QQ
微信
回复
2024-12-23 20:16:58
相关问题
HarmonyOS 关于http请求的返回结果
946浏览 • 1回复 待解决
HarmonyOS http请求封装
901浏览 • 1回复 待解决
HarmonyOS 取消http请求
1179浏览 • 1回复 待解决
HarmonyOS http请求的封装
949浏览 • 1回复 待解决
求告知如何发起HTTP请求
1276浏览 • 1回复 待解决
HarmonyOS 网络请求结果数据解析
815浏览 • 1回复 待解决
HarmonyOS http请求返回2300007
1892浏览 • 1回复 待解决
http 请求 如何cookie持久化?
2893浏览 • 1回复 待解决
HarmonyOS http post请求参数传递
1102浏览 • 1回复 待解决
HarmonyOS Http请求头问题咨询
1139浏览 • 1回复 待解决
http请求支持patch方法
3022浏览 • 1回复 待解决
HarmonyOS http请求如何自定义报文
614浏览 • 1回复 待解决
HTTP GET请求如何传递参数?
4146浏览 • 1回复 待解决
HarmonyOS http get请求不到内容
1141浏览 • 1回复 待解决
HarmonyOS http 请求 post 参数问题
2382浏览 • 1回复 待解决
HarmonyOS 如何取消一个HTTP请求
683浏览 • 1回复 待解决
如何使用arkts进行http请求通信
274浏览 • 0回复 待解决
http请求报错2300006如何解决
3960浏览 • 1回复 待解决
求大佬告知如何将http文件上传
1598浏览 • 1回复 待解决