我在httpc库中,在cHTTP_REQUEST_INFO.content = json_data;(代码在41-48)赋值但实际服务器无法收到,请问content中的内容是在body里还是header中,还是我使用方式有问题?
Code: Select all
#include "app_elec_zendure_http.h"
/***********************************************************
********************** variable define *********************
***********************************************************/
http_req_t cHTTP_REQUEST_INFO = {
.type = HTTP_POST,
.resource = APIresource,
.version = HTTP_VER_1_1,
.content = NULL,
.content_len = 0,
};
/***********************************************************
********************** function define *********************
***********************************************************/
/**
* @brief http task
*
* @param[in] param:Task parameters
* @return none
*/
int* example_http(INT_T argc, CHAR_T *argv[])
{
const CHAR_T *deviceID;
OPERATE_RET rt = OPRT_OK;
GW_WIFI_NW_STAT_E network_status = STAT_LOW_POWER;
int http_error = WM_SUCCESS;
http_session_t session = NULL;
http_resp_t *response = NULL;
http_req_t request;
//int default_frequency = 5; // 默认频率值
int default_result[6] = {5, 0, 2, 20, 0 ,5}; // 默认结果数组
TUYA_CALL_ERR_LOG(get_wf_gw_nw_status(&network_status));
if(network_status != STAT_CLOUD_CONN) {
TAL_PR_ERR("device not connect cloud!");
return default_result;
}
deviceID=get_gw_dev_id();
ty_cJSON *rootDevice = ty_cJSON_CreateObject();
ty_cJSON_AddStringToObject(rootDevice, "deviceId", deviceID);
char *json_data = ty_cJSON_PrintUnformatted(rootDevice);
cHTTP_REQUEST_INFO.content = json_data;
cHTTP_REQUEST_INFO.content_len = strlen(json_data);
ty_cJSON_Delete(rootDevice);
TAL_PR_DEBUG("Zendure example_http deviceId:%s\n",json_data);
memset((UCHAR_T *)&request, 0x00, SIZEOF(http_req_t));
http_error = http_open_session(&session, API, 0, 0);
if (http_error != WM_SUCCESS) {
TAL_PR_ERR("Failed to open an HTTP session err:%d", http_error);
return default_result;
}
http_error = http_prepare_req(session, &cHTTP_REQUEST_INFO, HDR_ADD_DEFAULT_USER_AGENT);
if (http_error != WM_SUCCESS) {
TAL_PR_ERR("Failed to prepare request infomation err:%d", http_error);
http_close_session(&session);
return default_result;
}
http_error = http_send_request(session, &request, 0);
if (http_error != WM_SUCCESS) {
TAL_PR_ERR("Failed to send request infomation err:%d", http_error);
http_close_session(&session);
return default_result;
}
// 获取HTTP响应头
http_error = http_get_response_hdr(session, &response);
if (http_error != WM_SUCCESS) {
TAL_PR_DEBUG("Failed to get HTTP response header err:%d", http_error);
http_close_session(&session);
return default_result;
}
// 读取HTTP响应内容
CHAR_T *p_buffer = (CHAR_T *)tal_malloc(READ_CONTENT_BUFF_LEN);
if(NULL == p_buffer) {
http_close_session(&session);
return default_result;
}
memset(p_buffer, 0, READ_CONTENT_BUFF_LEN);
http_read_content(session, p_buffer, READ_CONTENT_BUFF_LEN);
// 检查HTTP响应状态码
if (response->status_code != HTTP_OK) {
TAL_PR_DEBUG("HTTP request failed with status code: %d\n", response->status_code);
TAL_PR_DEBUG("%s\n", p_buffer);
tal_free(p_buffer);
http_close_session(&session);
return default_result;
}
// 解析JSON响应
ty_cJSON *root = ty_cJSON_Parse((const char*)p_buffer);
tal_free(p_buffer);
if (root == NULL) {
TAL_PR_DEBUG("Zendure Failed to parse JSON response\n");
http_close_session(&session);
return default_result;
}
// 提取所需的JSON数据
ty_cJSON *data = ty_cJSON_GetObjectItem(root, "data");
ty_cJSON *realtime = ty_cJSON_GetObjectItem(data, "realtime");
ty_cJSON *msg = ty_cJSON_GetObjectItem(root, "msg");
ty_cJSON *threshold = ty_cJSON_GetObjectItem(realtime, "threshold");
// 打印JSON数据
TAL_PR_DEBUG("Frequency: %d\n", ty_cJSON_GetObjectItem(realtime, "frequency")->valueint);
TAL_PR_DEBUG("Current Offset: %d\n", ty_cJSON_GetObjectItem(threshold, "current_offset")->valueint);
TAL_PR_DEBUG("Voltage Offset: %d\n", ty_cJSON_GetObjectItem(threshold, "voltage_offset")->valueint);
TAL_PR_DEBUG("Watt Offset: %d\n", ty_cJSON_GetObjectItem(threshold, "watt_offset")->valueint);
TAL_PR_DEBUG("Strategy_selection: %d\n", ty_cJSON_GetObjectItem(data, "strategy_selection")->valueint);
TAL_PR_DEBUG("Power_range: %d\n", ty_cJSON_GetObjectItem(data, "power_range")->valueint);
TAL_PR_DEBUG("Message: %s\n", msg->valuestring);
int *result = (int *)malloc(6 * sizeof(int)); // 分配内存
if (result == NULL) {
// 处理内存分配失败的情况
TAL_PR_DEBUG("Error:malloc error\n");
return default_result;
}
result[0]=ty_cJSON_GetObjectItem(realtime, "frequency")->valueint;
result[1]=ty_cJSON_GetObjectItem(threshold, "current_offset")->valueint;
result[2]=ty_cJSON_GetObjectItem(threshold, "voltage_offset")->valueint;
result[3]=ty_cJSON_GetObjectItem(threshold, "watt_offset")->valueint;
result[4]=ty_cJSON_GetObjectItem(data, "strategy_selection")->valueint;
result[5]=ty_cJSON_GetObjectItem(data, "power_range")->valueint;
// 释放资源
ty_cJSON_Delete(root);
http_close_session(&session);
return result;
}