Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:46:55+00:00 2026-06-15T23:46:55+00:00

I have homework where I need somehow to compare two HTTP responses. I am

  • 0

I have homework where I need somehow to compare two HTTP responses. I am writing it on C and I use libcurl to make things easier. I am calling the function that uses libcurl to do a HTTP request and response from another function, and I want to return the HTTP response as a char *. Here is my code so far (it crashes):

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

size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

char *handle_url(void) {
    CURL *curl;
    char *fp;
    CURLcode res;
    char *url = "http://www.yahoo.com";
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        if(res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %s\n",   curl_easy_strerror(res));

        curl_easy_cleanup(curl);

        //printf("\n%s", fp);
    }
    return fp;
}

This solution C libcurl get output into a string works, but not in my case because I just want to return the string to the calling function.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:46:56+00:00Added an answer on June 15, 2026 at 11:46 pm

    Fixed it for you. You need to handle the case where the write_data() function is called multiple times, and pass it the right kind of parameter. You also need to keep track of how big a structure you’ve got, so you can allocate enough memory.

    I left in a debug printf in the write_data function to help you understand how it works.

    #include <stdio.h>
    #include <curl/curl.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct url_data {
        size_t size;
        char* data;
    };
    
    size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) {
        size_t index = data->size;
        size_t n = (size * nmemb);
        char* tmp;
    
        data->size += (size * nmemb);
    
    #ifdef DEBUG
        fprintf(stderr, "data at %p size=%ld nmemb=%ld\n", ptr, size, nmemb);
    #endif
        tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */
    
        if(tmp) {
            data->data = tmp;
        } else {
            if(data->data) {
                free(data->data);
            }
            fprintf(stderr, "Failed to allocate memory.\n");
            return 0;
        }
    
        memcpy((data->data + index), ptr, n);
        data->data[data->size] = '\0';
    
        return size * nmemb;
    }
    
    char *handle_url(char* url) {
        CURL *curl;
    
        struct url_data data;
        data.size = 0;
        data.data = malloc(4096); /* reasonable size initial buffer */
        if(NULL == data.data) {
            fprintf(stderr, "Failed to allocate memory.\n");
            return NULL;
        }
    
        data.data[0] = '\0';
    
        CURLcode res;
    
        curl = curl_easy_init();
        if (curl) {
            curl_easy_setopt(curl, CURLOPT_URL, url);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
            res = curl_easy_perform(curl);
            if(res != CURLE_OK) {
                    fprintf(stderr, "curl_easy_perform() failed: %s\n",  
                            curl_easy_strerror(res));
            }
    
            curl_easy_cleanup(curl);
    
        }
        return data.data;
    }
    
    int main(int argc, char* argv[]) {
        char* data;
    
        if(argc < 2) {
            fprintf(stderr, "Must provide URL to fetch.\n");
            return 1;
        }
        data = handle_url(argv[1]);
    
        if(data) {
            printf("%s\n", data);
            free(data);
        }
    
        return 0;
    }
    

    Note: compile with gcc -o test test.c -lcurl (assuming you pasted into test.c). Use gcc -o test test.c -lcurl -DDEBUG to see the test printf() calls.

    Disclaimer: this is ugly, quick-and-dirty code. There may be bugs. Please see the more robust, better commented example here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this homework problem where I need to use regex to remove every
Hello I have a homework assignment where I need to read two matrix .txt
I have homework which I have to use scriptlets in , I need to
I have homework that I need to make a custom string class using C
I have a homework assignment where I need to take input from a file
Hi I have a homework assignment where I need to implement an intersection of
I have a homework and I need to evaluate which approach is better according
I need little help on a homework assignment. I have to create a 10
I have been given a homework assignment where I need to create a captcha
I have an assignment on 8086 assembly (this is homework), I need to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.