this post was submitted on 29 Nov 2024
-10 points (18.8% liked)

Programming

17742 readers
411 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
 

I have tried googling, and found no solution to my problem. I'm trying to learn how to use libcurl, a c networking library. I tried compiling a program that was automatically generated from curl, and a few examples i found online but nothing happened. I got no errors or logs, the program stopped "sucessfully" but i get no output. I also cant write to the console either while the library is included.

Any help is appreciated.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 1 month ago (2 children)

Ah, right.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct MemoryStruct {
    char *memory;
    size_t size;
};

size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, struct MemoryStruct *userp) {
    size_t realsize = size * nmemb;
    userp->memory = realloc(userp->memory, userp->size + realsize + 1);
    if(userp->memory == NULL) {
        return 0;
    }
    memcpy(&(userp->memory[userp->size]), contents, realsize);
    userp->size += realsize;
    userp->memory[userp->size] = 0;
    return realsize;
}

int main(int argc, char *argv[]) {
    CURLcode ret;
    CURL *hnd;
    struct MemoryStruct chunk;

    chunk.memory = malloc(1);
    chunk.size = 0;

    hnd = curl_easy_init();
    if(hnd) {
        curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
        curl_easy_setopt(hnd, CURLOPT_URL, "https://pastebin.com/api/api_post.php");
        curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
        curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "api_dev_key=API_KEY_HERE&api_paste_code=test&api_option=paste");
        curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)81);
        curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/8.9.1");
        curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
        curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
        curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
        curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(hnd, CURLOPT_WRITEDATA, (void *)&chunk);

        ret = curl_easy_perform(hnd);

        if(ret != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(ret));
        } else {
            printf("%s\n", chunk.memory);
        }

        curl_easy_cleanup(hnd);
        free(chunk.memory);
    } else {
        fprintf(stderr, "Failed to initialize CURL\n");
    }

    return (int)ret;
}

Finished it, i think. Still does "nothing"

edit: probably shouldn't include my api key with it.

[–] [email protected] 2 points 1 month ago* (last edited 1 month ago) (1 children)

I'm on mobile so I can't compile this myself, but can you clarify on what you're observing? Does "nothing" mean no output to stdout and stderr? Or that you did get an error message but it's not dispositive as to what libcurl was doing? Presumably the next step would be to validate that the program is executing at all, either with a debugger or printf-style debug statements at all junctures.

Please include as much detail as you can, since this is now more akin to a bug report.

EDIT: wait a sec. What exactly is this example code meant to do? The Pastebin API call suggests that this is meant to upload a payload to the web, not pull it down. But CURLOPT_WRITEFUNCTION is for receiving data from a URI. What is your intention with running this example program?

[–] [email protected] 1 points 1 month ago

I'm trying to send a post request to Pastebins api to make a paste. This is one of the first programs I have tried to write with libcurl, so its probably wrong.

[–] [email protected] 1 points 1 month ago (1 children)

What exactly are you expecting to happen? Have you written code before?

[–] [email protected] 1 points 1 month ago

I have written code before. I just started with C recently. I am expecting it to do literally anything at this point, but for some reason whenever I use the libcurl library literally nothing happens that i can observe.