-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqurl_http_post_demo.c
More file actions
227 lines (189 loc) · 6.97 KB
/
Copy pathqurl_http_post_demo.c
File metadata and controls
227 lines (189 loc) · 6.97 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*****************************************************************/ /**
* @file qurl_http_post_demo.c
* @brief
* @author harry.li@quectel.com
* @date 2025-05-7
*
* @copyright Copyright (c) 2023 Quectel Wireless Solution, Co., Ltd.
* All Rights Reserved. Quectel Wireless Solution Proprietary and Confidential.
*
* @par EDIT HISTORY FOR MODULE
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2025-05-7 <td>1.0 <td>harry.li <td> Init
* </table>
**********************************************************************/
#include "qosa_sys.h"
#include "qosa_def.h"
#include "qosa_log.h"
#include "qurl.h"
#include "unirtos_app_init_registry.h"
#include "qosa_virtual_file.h"
#define QOS_LOG_TAG LOG_TAG
#define UNIR_HTTP_DEMO_TASK_STACK_SIZE 4096
#define UNIR_HTTP_DEMP_PDPID 1
#define QURL_HTTP_APP_MAX(a, b) (((a) > (b)) ? (a) : (b))
#define QURL_HTTP_APP_MIN(a, b) (((a) < (b)) ? (a) : (b))
struct qurl_app_r_buf_s
{
char *buf_ptr;
long buf_len;
long r_index;
};
typedef struct qurl_app_r_buf_s qurl_app_r_buf_t;
static char g_form_data2[] = "this is test2";
static long qurl_http_app_w_h_cb(char *buf, long size, void *arg)
{
// Prevent unused parameter warning
QOSA_UNUSED(arg);
// Record received data size and content to application log
QLOGD("size=%d,%s", size, buf);
return size;
}
/**
* @brief HTTP application layer read form data callback function
*
* @param buf Buffer pointer for storing read data
* @param size Requested read data size
* @param arg Pointer to user-passed data
*
* @return Actual uploaded data size, returns 0 indicates no data to read
*/
long qurl_http_app_r_form_cb(unsigned char *buf, long size, void *arg)
{
qurl_app_r_buf_t *r_buf_ptr = (qurl_app_r_buf_t *)arg;
/* Check if requested write data size is valid */
if (size < 1)
{
return 0;
}
/* Calculate actual writable data size to avoid exceeding buffer boundaries */
size = QURL_HTTP_APP_MIN(size, r_buf_ptr->buf_len - r_buf_ptr->r_index);
/* Copy data from buffer to target buffer */
qosa_memcpy(buf, r_buf_ptr->buf_ptr + r_buf_ptr->r_index, size);
/* Update write index position */
r_buf_ptr->r_index += size;
/* All written */
if (r_buf_ptr->r_index >= r_buf_ptr->buf_len)
{
r_buf_ptr->r_index = 0;
}
return size;
}
static long qurl_http_app_w_cb(char *buf, long size, void *arg)
{
QOSA_UNUSED(arg);
QLOGD("size=%d,%s", size, buf);
return size;
}
/**
* @brief Execute HTTP POST form upload operation, demonstrating uploading form data through different methods.
*
* This function uses the qurl library to complete an HTTP POST request, uploading multiple form fields including:
* - Directly uploading string data;
* - Uploading data from global variables;
* - Uploading data through callback functions.
*
* Also sets response header and response body callback processing functions, and prints response status code and content length after request completion.
*
* @return qurl_ecode_t Execution result status code, QURL_OK indicates success.
*/
qurl_ecode_t qurl_http_app_post_form(void)
{
qurl_ecode_t ret = QURL_OK;
qurl_core_t core = QOSA_NULL;
long resp_code = 0;
long content_length = 0;
qurl_http_form_cfg_t form_cfg = {0};
qurl_app_r_buf_t r_buff = {0};
// Initialize global resources
qurl_global_init();
// Create core handle
ret = qurl_core_create(&core);
if (ret != QURL_OK)
{
QLOGE("%x\r\n", ret);
}
// Set target URL
ret = qurl_core_setopt(core, QURL_OPT_URL, "http://httpbin.org/post");
if (ret != QURL_OK)
{
QLOGE("%x\r\n", ret);
}
// Set network ID and POST form mode
qurl_core_setopt(core, QURL_OPT_NETWORK_ID, UNIR_HTTP_DEMP_PDPID);
qurl_core_setopt(core, QURL_OPT_HTTP_POST_FORM, 1L);
// Add first form field: directly upload string data
form_cfg.name_ptr = "test1";
form_cfg.filename_ptr = "file1";
form_cfg.content_type = QURL_HTTP_FORM_CONTENT_DATA;
form_cfg.content_ptr = "this is test1";
form_cfg.read_content_func = QOSA_NULL;
form_cfg.content_len = qosa_strlen(form_cfg.content_ptr);
qurl_core_setopt(core, QURL_OPT_FORM, 1L, &form_cfg);
// Add second form field: upload string data from global variables
form_cfg.name_ptr = "test2";
form_cfg.filename_ptr = "file2";
form_cfg.content_type = QURL_HTTP_FORM_CONTENT_DATA;
form_cfg.content_ptr = g_form_data2;
form_cfg.read_content_func = QOSA_NULL;
form_cfg.content_len = qosa_strlen(g_form_data2);
qurl_core_setopt(core, QURL_OPT_FORM, 2L, &form_cfg);
// Add third form field: upload data through callback function
r_buff.buf_ptr = qosa_malloc(20);
qosa_memset(r_buff.buf_ptr, 0, 20);
qosa_snprintf(r_buff.buf_ptr, 19, "%s", "this is test3");
r_buff.r_index = 0;
r_buff.buf_len = qosa_strlen(r_buff.buf_ptr);
form_cfg.name_ptr = "test3";
form_cfg.filename_ptr = "file3";
form_cfg.content_type = QURL_HTTP_FORM_CONTENT_CB;
form_cfg.read_content_func = qurl_http_app_r_form_cb;
form_cfg.content_ptr = &r_buff;
form_cfg.content_len = r_buff.buf_len;
qurl_core_setopt(core, QURL_OPT_FORM, 3L, &form_cfg);
// Set response header and response body callback processing functions
qurl_core_setopt(core, QURL_OPT_WRITE_HEAD_CB, qurl_http_app_w_h_cb);
qurl_core_setopt(core, QURL_OPT_WRITE_CB, qurl_http_app_w_cb);
// Execute HTTP request
ret = qurl_core_perform(core);
if (ret != QURL_OK)
{
QLOGE("%x\r\n", ret);
}
// Execute again (possibly for retry or debugging)
qurl_core_perform(core);
// Get response status code
qurl_core_getinfo(core, QURL_INFO_RESP_CODE, &resp_code);
QLOGV("resp_code:[%d]\r\n", resp_code);
// Get response content length
qurl_core_getinfo(core, QURL_INFO_RESP_CONTENT_LENGTH, &content_length);
QLOGV("content_length:[%d]\r\n", content_length);
// Delete core handle, release resources
ret = qurl_core_delete(core);
if (ret != QURL_OK)
{
QLOGE("%x\r\n", ret);
}
qosa_free(r_buff.buf_ptr);
return QURL_OK;
}
static void unir_http_post_app_init(void *argv)
{
qurl_ecode_t ret = QURL_OK;
qosa_task_sleep_sec(10);
ret = qurl_http_app_post_form();
QLOGE("ret=%d", ret);
}
void unir_qurl_http_post_demo_init(void)
{
int err = 0;
qosa_task_t http_task = QOSA_NULL;
err = qosa_task_create(&http_task, UNIR_HTTP_DEMO_TASK_STACK_SIZE, QOSA_PRIORITY_NORMAL, "http_post_demo", unir_http_post_app_init, QOSA_NULL);
if (err != QOSA_OK)
{
QLOGE("task create error");
return;
}
}
UNIRTOS_APP_EXPORT(364, "qurl_http_post_demo", unir_qurl_http_post_demo_init);