Emscripten c++ emscripten_fetch (and javascript version)

Viewed 29

I', trying to do a emscripten_fetch() and always get a return value of 0. Not sure what is going on, I switched to a javascript version, which works but has it's own problems.

C++ - can anyone see what I'm doing wrong here? To me, it looks like the header data isn't getting set properly, as I get this error:

Uncaught TypeError: XMLHttpRequest.open: Cannot convert argument 1 to ByteString because the character at index 2 has value 56770 which is greater than 255.

Here is the code:

  emscripten_fetch_attr_t attr;
  emscripten_fetch_attr_init(&attr);

  strcpy(attr.requestMethod, "POST");
  attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_WAITABLE;

  const char *headers[] = {"Content-Type", "application/json", 0};
  attr.requestHeaders = headers;

  attr.requestData = request.c_str() ;
  attr.requestDataSize = request.length() ;
    
qDebug("making fetch with URL: %s  data: %s", req.c_str(), attr.requestData) ;
  emscripten_fetch_t *fetch = emscripten_fetch(&attr, "http://172.23.4.90/jsonrpc"); // Blocks here until the operation is complete.

  EMSCRIPTEN_RESULT ret = EMSCRIPTEN_RESULT_TIMED_OUT;
  while(ret == EMSCRIPTEN_RESULT_TIMED_OUT)
  {
     // possibly do some other work;
      ret = emscripten_fetch_wait(fetch, 5000) ; // milliseconds to wait, 0 to just poll, INFINITY=wait until completion
  }

  if (fetch->status == 200) {
qDebug("Finished downloading %llu bytes from URL %s.", fetch->numBytes, fetch->url);
    // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];

    retval.append(fetch->data, fetch->numBytes) ;

  } else {
qDebug("Downloading %s failed, HTTP failure status code: %d.", fetch->url, fetch->status);
//        throw jsonrpccxx::JsonRpcException(-32003, "client connector error, received status != 200");
  }
  emscripten_fetch_close(fetch);

I've tried with the wait, and a full sync with the same issue.

As a result, I then tried writing it in javascript, and magic, it works! console.log() put out the results I am expecting. But, all my C++ program gets back is an object promise. I'm not a Javascript guy, and don't know what's going on.

So, my questions:

  1. does emscripten_fetch() work with POST header data?
  2. Can someone show me a javascript (EM_JS) version that works?

Gerald

0 Answers
Related