Send request to POSTMAN Mock server

49 views Asked by At

I'm doing HTTP request with nRF9160 DK board and POSTMAN MOCK server. My goal is send HTTP request from nRF9160 DK board, and check HTTP request from POSTMAN mock server. I'm using nRF for 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;
    }
}

Here is my send request() function, and button_handler() function. when I press the button 1, want to send request to mock server.

I try to handle 'req' to send data to mock server, but It didn't work well. Also Mock Server didn't get any request from client too.

[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

This is Terminal log what I got.

Any kind of advice is appreciated

Thank you.

0

There are 0 answers