Home > Blockchain >  Node Libcurl (node-libcurl) - trouble with multipart/form-data request
Node Libcurl (node-libcurl) - trouble with multipart/form-data request

Time:08-18

I have a particularly stubborn api that I've only been able to get to work with the following curl request:

curl --request PUT -H "Content-Type: multipart/form-data" -H "Authorization: Bearer abcd" -F functionConfig='{"parallelism":2};type=application/json' http://test-pulsar:8080/admin/v3/functions/test/ingest/test-entity-FeedTransformer

Here is the --libcurl output

#include <curl/curl.h>

int main(int argc, char *argv[])
{
  CURLcode ret;
  CURL *hnd;
  curl_mime *mime1;
  curl_mimepart *part1;
  struct curl_slist *slist1;

  mime1 = NULL;
  slist1 = NULL;
  slist1 = curl_slist_append(slist1, "Content-Type: multipart/form-data");
  slist1 = curl_slist_append(slist1, "Authorization: Bearer abcd");

  hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
  curl_easy_setopt(hnd, CURLOPT_URL, "http://test-pulsar.com:8080/admin/v3/functions/test/ingest/test-entity-FeedTransformer");
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  mime1 = curl_mime_init(hnd);
  part1 = curl_mime_addpart(mime1);
  curl_mime_data(part1, "{\"parallelism\":2}", CURL_ZERO_TERMINATED);
  curl_mime_name(part1, "functionConfig");
  curl_mime_type(part1, "application/json");
  curl_easy_setopt(hnd, CURLOPT_MIMEPOST, mime1);
  curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.79.1");
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
  curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "PUT");
  curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
  curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);

  /* Here is a list of options the curl code used that cannot get generated
     as source easily. You may select to either not use them or implement
     them yourself.

  CURLOPT_WRITEDATA set to a objectpointer
  CURLOPT_INTERLEAVEDATA set to a objectpointer
  CURLOPT_WRITEFUNCTION set to a functionpointer
  CURLOPT_READDATA set to a objectpointer
  CURLOPT_READFUNCTION set to a functionpointer
  CURLOPT_SEEKDATA set to a objectpointer
  CURLOPT_SEEKFUNCTION set to a functionpointer
  CURLOPT_ERRORBUFFER set to a objectpointer
  CURLOPT_STDERR set to a objectpointer
  CURLOPT_HEADERFUNCTION set to a functionpointer
  CURLOPT_HEADERDATA set to a objectpointer

  */

  ret = curl_easy_perform(hnd);

  curl_easy_cleanup(hnd);
  hnd = NULL;
  curl_mime_free(mime1);
  mime1 = NULL;
  curl_slist_free_all(slist1);
  slist1 = NULL;

  return (int)ret;
}

I believe the main part I'm stumped on is how to convert the following:

curl_mime_data(part1, "{\"parallelism\":2}", CURL_ZERO_TERMINATED);
curl_mime_name(part1, "functionConfig");
curl_mime_type(part1, "application/json");

What i've tried:

curl.setOpt(Curl.option.HTTPPOST, [
  {
    name: "functionConfig",
    contents: '{"parallelism":1};type=application/json',
  },
]);

But the api server doesn't recognize the functionConfig

{"reason":"Function config is not provided"}

How to resolve?

CodePudding user response:

node-libcurl author here. I will post the same answer I posted in the original issue: The curl mime APIs are not available, see https://github.com/JCMais/node-libcurl/issues/112 for the feature request.

What you can use is the HTTPPOST option (which has been deprecated, but is still working).

It would be something like this:

curl.setOpt(Curl.option.HTTPPOST, [
    { name: 'functionConfig', contents: '{"parallelism":2}'}
]);

You do lose the content-type in the above example. If that is important to the server you are using, the only way to set it is by saving the data to a temporary file and using that file as the source of the data:

curl.setOpt(Curl.option.HTTPPOST, [
    { name: 'functionConfig', file: '/path-to-temp-file', type: 'application/json' }
]);

Let me know if that does not work.

  • Related