0

I have cURL command:

curl "https://pelevin.gpt.dobro.ai/generate/" --data-binary '{"prompt" : "text" , "length" : 40}

and i want to do the same thing in C++.

I tried:

  string params = "prompt=text&length=40";
  string buffer;
  CURL*  curl;
  curl = curl_easy_init();
  if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://pelevin.gpt.dobro.ai/generate/");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, params.size());
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params.c_str());
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_perform(curl);
  }
  curl_easy_cleanup(curl);

and got message like this:

{"detail":[{"loc":["body",0],"msg":"Expecting value: line 1 column 1 (char 0)","type":"value_error.jsondecode","ctx":{"msg":"Expecting value","doc":"prompt=text&length=40","pos":0,"lineno":1,"colno":1}}]}

How can the request be made correctly?

1 Answer 1

1

When you went from command-line to libcurl, you changed the data you provide. Why?

First it was a JSON object string (which seems to be what the server is expecting). Now you've made it an URL-encoded string.

Just pass the right data to the server, as you did in the first example.

string params = "{\"prompt\" : \"text\" , \"length\" : 40}";

N.B. there are "better" ways to format that in modern C++, if you're up for it:

string params = R"RAW({"prompt" : "text" , "length" : 40})RAW";

BTW, it's weird to use --data-binary with data specified on the command-line. It's intended for use with file input, as it prevents newlines being stripped from the file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.