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: Timothe Litt <litt_at_acm.org>
Date: Fri, 21 Jan 2022 10:35:29 -0500

P.S. For those who are casual followers, Gavin is attempting to use the
jansson JSON library. https://github.com/akheron/jansson

It's a good library for C users of JSON.  It supports parsing and
generating JSON.

Timothe Litt
ACM Distinguished Engineer
--------------------------
This communication may not represent the ACM or my employer's views,
if any, on the matters discussed.

On 21-Jan-22 10:00, Gavin Henry via curl-library wrote:
> 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