HarmonyOS 鸿蒙Next HTTP Post 请求下的body参数怎么写呢
HarmonyOS 鸿蒙Next HTTP Post 请求下的body参数怎么写呢
接口信息如上
我这样写的不行,后台提醒我密码为空。
4 回复
请求头的content-type有改不
光看这一部分代码看不出来,ps是string还是object,string的话试一下改成data:{“password”:ps}
更多关于HarmonyOS 鸿蒙Next HTTP Post 请求下的body参数怎么写呢的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
搞定了, this.extraData = "password=" + ps
这样写后台收到了 。data:{"password":ps}
这种方式我试过 不行。
options是这样
{
"method": "GET",
"extraData": "password=ps",
"header": {
"key": "Accept-Language",
"value": "zh-TW",
"type": "text",
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic YXBwOmFwcA=="
},
"readTimeout": 50000,
"connectTimeout": 50000
}
在HarmonyOS(鸿蒙)中,使用HTTP Post请求时,body
参数通常以string
或ArrayBuffer
形式传递。以下是具体写法:
-
使用
string
格式的body
参数:let url = 'https://example.com/api'; let body = JSON.stringify({ key1: 'value1', key2: 'value2' }); let options = { method: 'POST', header: { 'Content-Type': 'application/json' }, body: body }; fetch.fetch(url, options).then(response => { console.log(response); });
-
使用
ArrayBuffer
格式的body
参数:let url = 'https://example.com/api'; let body = new ArrayBuffer(16); // 示例ArrayBuffer let options = { method: 'POST', header: { 'Content-Type': 'application/octet-stream' }, body: body }; fetch.fetch(url, options).then(response => { console.log(response); });
在以上代码中,body
参数根据实际需求选择string
或ArrayBuffer
格式,并通过fetch
方法发送Post请求。
在HarmonyOS中,使用HTTP Post请求时,可以通过RequestParams
对象设置body参数。以下是一个示例:
RequestParams params = new RequestParams();
params.setUri("https://example.com/api");
params.setMethod(HttpMethod.POST);
params.setBodyContent("application/json", new StringEntity("{\"key\":\"value\"}", "UTF-8"));
HttpClient httpClient = new HttpClient();
httpClient.execute(params, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
// 处理成功响应
}
@Override
public void onError(HttpResponse response) {
// 处理错误响应
}
});
在这个示例中,setBodyContent
方法用于设置请求体,application/json
指定了内容类型,StringEntity
用于封装JSON格式的字符串。