Skip to content

Http client request within a RESTful server  #1653

Description

@f4r4
  • My goal is: To understand mongoose http client requests
  • My question is: I have a Restful server built using mongoose and I want to send a client http request to another server,
    I am using a new event manager (mgr) to do the request, I got the code from this example but still I have the issue of operation in progress could anyone please advice
0 3 mongoose.c:4219:write_conn          1 0x5 113:0 113 err 115 (Operation now in progress)
0 3 mongoose.c:4208:read_conn           1 0x5 0:0 211 err 115 (Operation now in progress)

my server code

  • main calls fn
  • fn calls client call client_request
  • client_request calls fnpost

// The very first web page in history. You can replace it from command line
static const char *s_url = "127.0.0.1:3000";
static const char *s_post_data = NULL;      // POST data
static const uint64_t s_timeout_ms = 1500;  // Connect timeout in milliseconds

// Print HTTP response and signal that we're done
void fnpost(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
  if (ev == MG_EV_OPEN) {
    // Connection created. Store connect expiration time in c->label
    *(uint64_t *) c->label = mg_millis() + s_timeout_ms;
  } else if (ev == MG_EV_POLL) {
    if (mg_millis() > *(uint64_t *) c->label &&
        (c->is_connecting || c->is_resolving)) {
      mg_error(c, "Connect timeout");
    }
  } else if (ev == MG_EV_CONNECT) {
    // Connected to server. Extract host name from URL
    struct mg_str host = mg_url_host(s_url);

    mjson_printf(&mjson_print_dynamic_buf, &s_post_data, "{\"result\": \"%s\"}\n", "new_data");

    // Send request
    int content_length = s_post_data ? strlen(s_post_data) : 0;
    mg_printf(c,
              "%s %s HTTP/1.0\r\n"
              "Host: %.*s\r\n"
              "Content-Type: application/json\r\n"
              "Content-Length: %d\r\n"
              "\r\n",
              s_post_data ? "POST" : "GET", mg_url_uri(s_url), (int) host.len,
              host.ptr, content_length);
    mg_send(c, s_post_data, content_length);
    //free(s_post_data);
  } else if (ev == MG_EV_HTTP_MSG) {
    // Response is received. Print it
    struct mg_http_message *hm = (struct mg_http_message *) ev_data;
    printf("%.*s\n", (int) hm->message.len, hm->message.ptr);
    c->is_closing = 1;         // Tell mongoose to close this connection
    *(bool *) fn_data = true;  // Tell event loop to stop
  } else if (ev == MG_EV_ERROR) {
    *(bool *) fn_data = true;  // Error, tell event loop to stop
  }
}

void client_request(){
  mg_log_set("3");          // Set to 0 to disable debug
  mg_mgr_init(&mgr);              // Initialise event manager
  mg_http_connect(&mgr, s_url, fnpost, &done);  // Create client connection
  while (!done) mg_mgr_poll(&mgr, 50);      // Event manager loops until 'done'
  mg_mgr_free(&mgr);                        // Free resources
}


static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
   (void)fn_data;
   if (ev == MG_EV_HTTP_MSG) {
      struct mg_http_message *hm = (struct mg_http_message *) ev_data;
      if(mg_http_match_uri(hm, "/api/version")){
        //some other code and I execute
        char *s = NULL;
        mjson_printf(&mjson_print_dynamic_buf, &s, "{\"result\": \"%s\"}\n", "1.0.0-beta");
        mg_http_reply(c, 200, "Content-Type: application/json\r\n", s);
        free(s);

        client_request();
      }
 }

int main(void) {
  struct mg_mgr mgr;
  mg_log_set("3");
  mg_mgr_init(&mgr);                                        // Init manager
  mg_http_listen(&mgr, "http://0.0.0.0:8010", fn, &mgr);  // Setup listener
  for (;;) mg_mgr_poll(&mgr, 1000);                         // Event loop
  mg_mgr_free(&mgr);                                        // Cleanup
  return 0;
}

Please advice

Environment

  • mongoose version: 7.7
  • Compiler/IDE and SDK: GCC 4.9.2
  • Target hardware/board: x86

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions