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 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

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1848
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T04:36:35+00:00 2022-05-31T04:36:35+00:00

c – How to use cURL to parse response body and then loop thought lines?

  • 0

[ad_1]

I wanna do first a call using cURL into a JSON and save the respond buffer body in array/memory and then to be able to loop thought this array/memory and get each saved line, then to do my comparisons, i tried to follow https://curl.se/libcurl/c/getinmemory.html cURL code example but im stuck in how to loop thought the saved ‘chunk.memory’ buffer to get each saved line, any help is appreciated.

I wanna do the loop under else { config_status("Received data: %*\n", chunk.memory); } where chunk.memory supposed to be the memory buffer there.

NOTE: I am a C beginner 😛

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

static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
 
  char *ptr = realloc(mem->memory, mem->size + realsize + 1);

  if (!ptr) {
    /* out of memory! */
    //printf("not enough memory (realloc returned NULL)\n");

    return 0;
  }
 
  mem->memory = ptr;

  memcpy(&(mem->memory[mem->size]), contents, realsize);

  mem->size += realsize;
  mem->memory[mem->size] = 0;
 
  return realsize;
}

int check_ip(char *ip) {
    CURL *curl;
    CURLcode res;

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

    char *url = "http://proxycheck.io/v2/204.225.96.123?key=10i9s3-685432-3f2774-t14562&vpn=1&asn=1&risk=1&port=1&days=7";

    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0");

        /*
        struct curl_slist *headers = NULL;

        headers = curl_slist_append(headers, "length: 20");
        headers = curl_slist_append(headers, "numbers: true");

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        */

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

        res = curl_easy_perform(curl);

        if (res != CURLE_OK) { config_error("curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); }
        else { config_status("Received data: %*\n", chunk.memory); }

        // response log file (by config_status() func)
        // Received data: {

        free(chunk.memory);      
    
        curl_easy_cleanup(curl);
    }

    return 0;
}

[ad_2]

  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4796 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1009 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

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.