提问人:Dear_ddyd 提问时间:11/10/2023 更新时间:11/10/2023 访问量:17
在zephyr OS的http请求中添加Optional标头(使用nRF9160DK)
Adding Optional header in http request in zephyr OS(using nRF9160DK)
问:
我尝试在我的 http 请求中放置可选标头,基于 zephyr OS。 我正在尝试在 zephyr OS 中使用 optional_header,但它效果不佳。
我试过这样。 这是我请求的函数和标头结构
const char *http_header_val[][2] = {
{"Content-type", "application//json"},
{"Accept", "application//json"},
{"Iccid", "8944501201210634639"},
{"ModemId", "860016042571781"},
{"Timestamp", "20231109113230"},
{"ZoneOffset", "+9"}
};
int send_request()
{
struct http_request req = {0};
const char *h_dd = "IccId";
req.method = HTTP_POST;
req.url = "/TEST";
req.host = SERVER_HOSTNAME;
req.port = SERVER_PORT;
req.protocol = "HTTP/1.1";
req.recv_buf = recv_buf;
req.recv_buf_len = sizeof(recv_buf);
req.response = response_cb;
req.optional_headers = http_header_val;
int err = http_client_req(sock4, &req, timeout, NULL);
//LOG_INF("HEADER \n\n\r%s", (req.header_fields));
return err;
}
/* press button for send request to server*/
static void button_handler(uint32_t button_state, uint32_t has_changed)
{
switch (has_changed) {
case DK_BTN1_MSK:
/* send http request */
if(button_state & DK_BTN1_MSK){
int err_2 = send_request();
if(err_2 <= 0){
LOG_INF("Failed to send message, %d", errno);
LOG_INF("Failed request, %d", err_2);
break;
}
}
break;
}
}
这是我的响应回调函数。
static void response_cb(struct http_response *rsp, enum http_final_call final_data, void *user_data)
{
if(rsp->http_status_code >= 200 && rsp->http_status_code < 300){
LOG_INF("Net status code: %d", rsp->http_status_code);
LOG_INF("Net status: %s", rsp->http_status);
LOG_INF("Get data size: %d", rsp->recv_buf_len);
LOG_INF("Get data : \n\r%s", rsp->recv_buf);
}else{
LOG_ERR("Net status code: %d", rsp->http_status_code);
LOG_ERR("Net status: %s", rsp->http_status);
LOG_INF("Get data : \n\r%s", rsp->recv_buf);
}
}
这是我的结果
*** Booting nRF Connect SDK v2.5.0 ***
[00:00:00.398,986] <inf> Socket_tel: Initializing modem library
[00:00:00.638,885] <inf> Socket_tel: Connecting to LTE network
[00:00:02.226,013] <inf> Socket_tel: RRC mode: Connected
[00:00:03.360,443] <inf> Socket_tel: Network registration status: Connected - roaming
[00:00:03.360,595] <inf> Socket_tel: Connected to LTE network
[00:00:03.891,815] <inf> Socket_tel: IPv4 Address found 34.198.21.227
[00:00:04.434,509] <inf> Socket_tel: Successfully connected to server
[00:00:04.434,539] <inf> Socket_tel: Press button 1 on your DK to send your message
[00:00:04.434,539] <inf> Socket_tel: Response
[00:00:09.440,399] <err> Socket_tel: Net status code: 400
[00:00:09.440,460] <err> Socket_tel: Net status: Bad Request
[00:00:09.440,551] <inf> Socket_tel: Get data :
HTTP/1.1 400 Bad Request
Server: awselb/2.0
Date: Fri, 10 Nov 2023 01:35:17 GMT
Content-Type: text/html
Content-Length: 122
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
</body>
</html>
我认为我的问题是添加 http 标头,但我不知道如何解决这个问题。
你的任何帮助,对我非常有帮助。
谢谢。
ddyd。
答: 暂无答案
评论