向 POSTMAN 模拟服务器发送请求

Send request to POSTMAN Mock server

提问人:Dear_ddyd 提问时间:11/3/2023 更新时间:11/3/2023 访问量:20

问:

我正在使用 nRF9160 DK 板和 POSTMAN MOCK 服务器执行 HTTP 请求。 我的目标是从 nRF9160 DK 板发送 HTTP 请求,并检查来自 POSTMAN 模拟服务器的 HTTP 请求。 我正在将 nRF 用于 VScode、Zephyr OS。

static int server_resolve(void)
{
    /* Call getaddrinfo() to get the IP address of the echo server */
    int err;
    struct addrinfo *result;
    struct addrinfo hints = {
        .ai_family = AF_INET,
        .ai_socktype = SOCK_STREAM
    };

    err = getaddrinfo(SERVER_HOSTNAME, SERVER_PORT, &hints, &result);
    if (err != 0) {
        LOG_INF("ERROR: getaddrinfo failed %d\n", err);
        return -EIO;
    }

    if (result == NULL) {
        LOG_INF("ERROR: Address not found\n");
        return -ENOENT;
    }

    /* Retrieve the relevant information from the result structure*/
    struct sockaddr_in *server4 = ((struct sockaddr_in *)&server);

    server4->sin_addr.s_addr =
        ((struct sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
    server4->sin_family = AF_INET;
    server4->sin_port = ((struct sockaddr_in *)result->ai_addr)->sin_port;

    /* Convert the address into a string and print it */
    char ipv4_addr[NET_IPV4_ADDR_LEN];
    inet_ntop(AF_INET, &server4->sin_addr.s_addr, ipv4_addr,
          sizeof(ipv4_addr));
    LOG_INF("IPv4 Address found %s", ipv4_addr);

    /* Free the memory allocated for result */
    freeaddrinfo(result);

    return 0;
}

static int server_connect(void)
{
    int err;
    /*reate a TCP socket */
    sock4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock4 < 0) {
        LOG_ERR("Failed to create socket4: %d.", errno);
        return -errno;
    }

    /* Connect the socket to the server */
    err = connect(sock4, (struct sockaddr *)&server,
              sizeof(struct sockaddr_in));
    if (err < 0) {
        LOG_ERR("Connect failed : %d", errno);
        return -errno;
    }
    LOG_INF("Successfully connected to server");


    return 0;
}

int send_request()
{
    struct http_request req = {0};
    
    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);

    int err = http_client_req(sock4, &req, timeout, "");
    
    return err;
}

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;
            }else{
                LOG_INF("Successfully sent message: %s", "");
                break;
            }
        }
        break;
    }
}

这是我的 send request() 函数和 button_handler() 函数。 当我按下按钮 1 时,想要向模拟服务器发送请求。

我尝试处理“req”以将数据发送到模拟服务器,但它效果不佳。 此外,模拟服务器也没有收到来自客户端的任何请求。

[00:00:00.382,598] <inf> Socket_tel: Initializing modem library
[00:00:00.622,528] <inf> Socket_tel: Connecting to LTE network
[00:00:06.174,346] <inf> Socket_tel: RRC mode: Connected
[00:00:07.628,814] <inf> Socket_tel: Network registration status: Connected - roaming
[00:00:07.628,936] <inf> Socket_tel: Connected to LTE network
[00:00:08.140,167] <inf> Socket_tel: IPv4 Address found 18.204.117.71
[00:00:08.674,835] <inf> Socket_tel: Successfully connected to server
[00:00:08.674,865] <inf> Socket_tel: Press button 1 on your DK or Thingy:91 to send your message
[00:00:20.011,505] <inf> Socket_tel: RRC mode: Idle
[00:01:08.960,205] <inf> Socket_tel: Failed to send message, 0
[00:01:08.960,235] <inf> Socket_tel: Failed request, -22

这是我得到的终端日志。

任何类型的建议都是值得赞赏的

谢谢。

httprequest 模拟服务器 zephyr-rtos nrf91

评论


答: 暂无答案