cURL / Mailing Lists / curl-library / Single Mail

curl-library

How to use a bash script for http-post?

From: Kman Devnull <kman.devnull_at_gmail.com>
Date: Wed, 25 May 2016 05:37:47 +0100

Hi,
My current curl code is in C, and runs within a gcc linux env. My current
code uses libcurl to do HTTP post.

Now, I want to use a bash script instead of the below C code to carry out
the same curl-http-post.

How do I use bash script to use CURL for HTTP-POST? Can you point me to any
examples, please?

Many thanks.

====== My code looks similar to the below =============
#*include* *<stdio.h>*

#*include* *<stdlib.h>*
#*include* *<string.h>*

#*include* *<curl/curl.h>*
 *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;

  mem->memory = realloc(mem->memory, mem->size + realsize + 1);
  *if*(mem->memory == NULL) {
    */* out of memory! */ *
    printf(*"not enough memory (realloc returned NULL)\n"*);
    *return* 0;
  }

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

  *return* realsize;
}
 *int* *main*(*void*)
{
  CURL *curl_handle;
  CURLcode res;

  *struct* MemoryStruct chunk;

  chunk.memory = malloc(1); */* will be grown as needed by the
realloc above */ *
  chunk.size = 0; */* no data at this point */ *

  curl_global_init
<https://curl.haxx.se/libcurl/c/curl_global_init.html>(CURL_GLOBAL_ALL);

  */* init the curl session */ *
  curl_handle = curl_easy_init
<https://curl.haxx.se/libcurl/c/curl_easy_init.html>();

  */* specify URL to get */ *
  curl_easy_setopt
<https://curl.haxx.se/libcurl/c/curl_easy_setopt.html>(curl_handle,
CURLOPT_URL <https://curl.haxx.se/libcurl/c/CURLOPT_URL.html>,
*"http://www.example.com/ <http://www.example.com/>"*);

  */* send all data to this function */ *
  curl_easy_setopt
<https://curl.haxx.se/libcurl/c/curl_easy_setopt.html>(curl_handle,
CURLOPT_WRITEFUNCTION
<https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html>,
WriteMemoryCallback);

  */* we pass our 'chunk' struct to the callback function */ *
  curl_easy_setopt
<https://curl.haxx.se/libcurl/c/curl_easy_setopt.html>(curl_handle,
CURLOPT_WRITEDATA
<https://curl.haxx.se/libcurl/c/CURLOPT_WRITEDATA.html>, (*void*
*)&chunk);

  */* some servers don't like requests that are made without a user-agent
     field, so we provide one */ *
  curl_easy_setopt
<https://curl.haxx.se/libcurl/c/curl_easy_setopt.html>(curl_handle,
CURLOPT_USERAGENT
<https://curl.haxx.se/libcurl/c/CURLOPT_USERAGENT.html>,
*"libcurl-agent/1.0"*);

  */* get it! */ *
  res = curl_easy_perform
<https://curl.haxx.se/libcurl/c/curl_easy_perform.html>(curl_handle);

  */* check for errors */ *
  *if*(res != CURLE_OK) {
    fprintf(stderr, *"curl_easy_perform
<https://curl.haxx.se/libcurl/c/curl_easy_perform.html>() failed:
%s\n"*,
            curl_easy_strerror
<https://curl.haxx.se/libcurl/c/curl_easy_strerror.html>(res));
  }
  *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!
     */ *

    printf(*"%lu bytes retrieved\n"*, (*long*)chunk.size);
  }

  */* cleanup curl stuff */ *
  curl_easy_cleanup
<https://curl.haxx.se/libcurl/c/curl_easy_cleanup.html>(curl_handle);

  free(chunk.memory);

  */* we're done with libcurl, so clean it up */ *
  curl_global_cleanup
<https://curl.haxx.se/libcurl/c/curl_global_cleanup.html>();

  *return* 0;
}

-------------------------------------------------------------------
List admin: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2016-05-25