cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: New to CURL - HTTP POST to web service and response

From: Sutton, Timothy <tsutton_at_cspeed.com>
Date: Thu, 18 Jun 2015 11:59:38 -0400

Ray-

I think this example will help you get the data back from the post, and
show you how to write a WriteMemoryCallback function.
http://curl.haxx.se/libcurl/c/getinmemory.html I attempted to clean up your
code to just perform the post, but didnt test this to ensure it works so
might have missed something. You will need to work on the
WriteMemoryCallback to handle the response of this message the way you
want.

res = curl_global_init(CURL_GLOBAL_ALL);

            /* Check for errors */

            if(res != CURLE_OK)

            {

                fprintf(stderr, "curl_global_init() failed: %s\n",

                curl_easy_strerror(res));

                return 1;

            }

            curl = curl_easy_init();

            if(curl)

            {

               printf("\n\n\nYou are using libcurl/%s\n\n\n",
curl_version_info(CURLVERSION_NOW)->version);

               curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

               /* specify target URL */

               curl_easy_setopt(curl, CURLOPT_URL, url);

               curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);

               curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);

              /*Response you are looking for in the post will appear in the
WriteMemoryCallback function*/

              curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);

               curl_easy_setopt(curl, CURLOPT_POSTFIELDS, full_msg);

               res = curl_easy_perform(curl);

               if(CURLE_OK != res)

               {

                   printf("Error: %s\n", strerror(res));

                   return 1;

               }

               /* always cleanup */

               curl_easy_cleanup(curl);

            }

Tim

On Thu, Jun 18, 2015 at 11:20 AM, Fitzgerald, Kevin <kevin.fitzgerald_at_hp.com
> wrote:

> Hi, sorry I was not detailed enough. Yes, I am using the POST example
> from the CURL repository. I originally was trying the sendrecv example. I
> replaced the send portion with the POST portion and was still using the
> receive part. Here is what I am getting with VERBOSE turned on (BTW, I did
> not understand VERBOSE, thanks for the info):
>
>
>
> You are using libcurl/7.28.1
>
>
>
>
>
> * About to connect() to acc.dwd.wisconsin.gov port 443 (#0)
>
> * Trying 167.218.117.18...
>
> * connected
>
> * Connected to acc.dwd.wisconsin.gov (167.218.117.18) port 443 (#0)
>
> * SSL connection using AES256-SHA
>
> * Server certificate:
>
> * subject: C=US; ST=Wisconsin; L=Madison; O=State of Wisconsin -
> DWD; OU=Administrative Services; CN=*.wisconsin.gov
>
> * start date: 2012-09-11 00:00:00 GMT
>
> * expire date: 2015-11-18 12:00:00 GMT
>
> * subjectAltName: acc.dwd.wisconsin.gov matched
>
> * issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert
> High Assurance CA-3
>
> * SSL certificate verify result: unable to get local issuer
> certificate (20), continuing anyway.
>
> * Connection #0 to host acc.dwd.wisconsin.gov left intact
>
> Sending request.
>
> * Re-using existing connection! (#0) with host acc.dwd.wisconsin.gov
>
> * Connected to acc.dwd.wisconsin.gov (167.218.117.18) port 443 (#0)
>
> * Connection #0 to host acc.dwd.wisconsin.gov left intact
>
> Reading response.
>
> Recieve Error: Socket not ready for send/recv
>
>
>
> It seems to be a problem with the socket. Here is the actual code:
>
>
>
> res = curl_global_init(CURL_GLOBAL_ALL);
>
> /* Check for errors */
>
> if(res != CURLE_OK)
>
> {
>
> fprintf(stderr, "curl_global_init() failed: %s\n",
>
> curl_easy_strerror(res));
>
> return 1;
>
> }
>
>
>
> curl = curl_easy_init();
>
> if(curl)
>
> {
>
> printf("\n\n\nYou are using libcurl/%s\n\n\n",
> curl_version_info(CURLVERSION_NOW)->version);
>
> curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
>
>
>
> /* specify target URL */
>
> curl_easy_setopt(curl, CURLOPT_URL, url);
>
>
>
> curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
>
>
>
> curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
>
> res = curl_easy_perform(curl);
>
>
>
> if(CURLE_OK != res)
>
> {
>
> printf("Error: %s\n", strerror(res));
>
> return 1;
>
> }
>
>
>
> /* Extract the socket from the curl handle - we'll need it
> for waiting.
>
> * Note that this API takes a pointer to a 'long' while we
> use
>
> * curl_socket_t for sockets otherwise.
>
> */
>
> res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET,
> &sockextr);
>
>
>
> if(CURLE_OK != res)
>
> {
>
> printf("Error: %s\n", curl_easy_strerror(res));
>
> return 1;
>
> }
>
>
>
> sockfd = sockextr;
>
>
>
> /* wait for the socket to become ready for sending */
>
> if(!wait_on_socket(sockfd, 0, 60000L))
>
> {
>
> printf("Error: timeout.\n");
>
> return 1;
>
> }
>
>
>
> curl_easy_setopt(curl, CURLOPT_POSTFIELDS, full_msg);
>
> /* Perform the request, res will get the return code */
>
> res = curl_easy_perform(curl);
>
> /* Check for errors */
>
> if(res != CURLE_OK)
>
> {
>
> fprintf(stderr, "curl_easy_perform() failed: %s\n",
>
> curl_easy_strerror(res));
>
> }
>
>
>
> puts("Reading response.");
>
>
>
> /* read the response */
>
> wait_on_socket(sockfd, 1, 60000L);
>
> res = curl_easy_recv(curl, s1, BUF_SZ9, &iolen);
>
>
>
> if(CURLE_OK != res)
>
> {
>
> printf("Recieve Error: %s\n", curl_easy_strerror(res));
>
> return 1;
>
> }
>
>
>
> nread = (curl_off_t)iolen;
>
> printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n",
> nread);
>
>
>
> /* always cleanup */
>
> curl_easy_cleanup(curl);
>
>
>
> }
>
>
>
> Thank you for your help…..
>
>
>
> Kevin Fitzgerald
>
> Provider SE
>
> Eligibility/SSI SE
>
> Healthcheck SE
>
> TPL SE
>
> Batch Promotion Coordinator
>
> HP Enterprise Services
>
> Work: 847-485-8060
>
> Cell: 847-204-5077
>
> kevin.fitzgerald_at_hp.com
>
>
>
> *From:* curl-library [mailto:curl-library-bounces_at_cool.haxx.se] *On
> Behalf Of *Ray Satiro via curl-library
> *Sent:* Thursday, June 18, 2015 1:42 AM
> *To:* curl-library_at_cool.haxx.se
> *Subject:* Re: New to CURL - HTTP POST to web service and response
>
>
>
> On 6/17/2015 3:12 PM, Fitzgerald, Kevin wrote:
>
> Hello, I am very new to CURL and I am trying to figure out the
> best/correct way to use CURL to perform the functions that I need to
> perform. I have written a C program running on an HP/UX box. I need to
> perform an HTTP POST to a web service and then receive a response back from
> the server. I have tried different things from examples I have seen, but so
> far I have not been successful. Is there an example out there that will
> show me what I need to code using CURL? I have found parts in different
> examples, but not one that does all that I need to do.
>
>
>
>
> Please be more specific. What examples are you working from? Do you get an
> error? Can you give us verbose output? Enable verbosity, also show the
> libcurl version your program is using:
>
> printf("\n\n\nYou are using libcurl/%s\n\n\n",
> curl_version_info(CURLVERSION_NOW)->version);
> curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
>
> There is a simple POST example in docs/examples/http-post.c [1].
>
>
> [1]: http://curl.haxx.se/libcurl/c/http-post.html
>
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html
>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-06-18