curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: CURLOPT_WRITEFUNCTION issues - warning: curl_easy_setopt expects a curl_write_callback argument

From: Gavin Henry via curl-library <curl-library_at_lists.haxx.se>
Date: Fri, 21 Jan 2022 15:00:30 +0000

Hi Timothe,

> I'm confused about what you're trying to do.

It seems I am too as I have fully understood the callback.

> outstream is an input parameter. It will contain whatever you set in the CURLOPT_WRITEDATA option.
>
> If it's a json structure that's input to your callback, overwriting the pointer with json_loadb doesn't make sense.

No, it doesn't now. What I'm trying to do is save the result of this,
then pull out the "version" and compare it to the PACKAGE_VERSION of
SentryPeer in my test suite. So if it was in bash or on the CLI:

curl -H "Content-Type: application/json" http://127.0.0.1:8082/health-check
{
  "status": "OK",
  "message": "Hello from SentryPeer!",
  "version": "0.0.6"
}


> In fact, the value of outstream, now in json isn't used - loadb simply overwrites it.
>
> Further, your callback may be called many times with pieces of the json string. You need to get the entire response before calling json_loadb, or use another call.
>
> You can use "outstream" as a pointer to a buffer that accumulates the response, or to a struct with the next write pointer and remaining count in that buffer, or ...
>
> You need to re-architect this code.

It's my misunderstanding of where to put json_loadb(). It's not
supposed to be done in the callback, but filling up a json buffer in
there and processing wherever I've set CURLOPT_WRITEDATA, correct?

As the callback, as you say, may not parse all of the above json in
one go or be given it from the API endpoint.

My goal is to be able save/process the returned JSON and pull out the
version string to do:

json_t *sentrypeer_version_json = json_object_get(json, "version");
assert_string_equal(json_string_value(sentrypeer_version_json),
PACKAGE_VERSION);

Thanks.
-- 
Unsubscribe: https://lists.haxx.se/listinfo/curl-library
Etiquette:   https://curl.haxx.se/mail/etiquette.html
Received on 2022-01-21