This is wandering off topic for curl.

json_loadb wants the full JSON string.

You can use the example previously cited, and call json_loadb where the transfer is complete.  Something like.

https://curl.se/libcurl/c/getinmemory.html

 *else*  {
//* * Now, our chunk.memory points to a memory block that is chunk.size * bytes big and contains the remote file. * * Do something nice with it! *//
   json_t response;
   json_error_t error;

    printf(*"%lu bytes retrieved at address %p\n"*, (*unsigned*  
*long*)chunk.size, chunk.memory);

        response = json_loadb(chunk.memory, chunk.size, 0, &error);

       if( !response ) {

        fprintf( stderr, "Response error: %s at line %d, column %d\n", error.text? error.text: "unspecified", error.line, error.column ); exit(1);

}

jjson_t * val = json_object_get( json, ...); const char *ver = json_string_value( val );

janson_decref(json); json = NULL;

And free the chunk.memory before exiting.

This is a long way from a compiler error - but note that every error you reported was a legitimate error in your code.

You need to be very, very careful about ignoring errors - while they don't catch the architectural errors, they almost always indicate a real problem - functional or portability.

Have fun.

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.

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

-- 
Unsubscribe: https://lists.haxx.se/listinfo/curl-library
Etiquette:   https://curl.haxx.se/mail/etiquette.html

Reply via email to