cURL / Mailing Lists / curl-users / Single Mail

curl-users

Login & FileUpload via HTTP POST faling - using curl_formadd

From: Krishna Murthy Gudipati <krishnamurthyg_at_infotechsw.com>
Date: Wed, 5 Nov 2008 21:59:54 +0530

Hello Friends,

 

I hv a requirement of connecting to a site and upload binary/text file.

 

From the MSDOS command prompt using curl.exe the login and upload is
perfectly working. But login and upload using libcurl from C++ application
is failing.

 

Platform is Windows XP

 

 ----- COMMAND LINE Curl EXECUTION BEGIN -----

 

######## LogIn ########

curl -c cookies.txt -A "PWCAutoUploader 0.1" -o
requestOutput/loginResult.html -d
"userName=x&password=y&portalUser=N&serviceCompanyId=-1"
http://utcit-190:9999/webectm/logon.do

 

######## UpLoad ########

curl -b cookies.txt -A "PWCAutoUploader 0.1" -o
requestOutput/uploadResult.html -F "type=GBSStandardFile" -F
"theFile=@GK-Nov-05-1-D004000_.SSS"
http://utcit-190:9999/webectm/loadEngineFileToDB.do

 

 ----- COMMAND LINE Curl EXECUTION END -----

 

The same above operations of login and upload when done using libcurl in C++
program the login process is success but the upload is failing. The upload
is http post method and i a using the sample code explained in
http://curl.haxx.se/libcurl/c/libcurl-tutorial.html in HTTP POSTing section.
Please suggest me if i am doing anthing wrong. The C++ code is below :

 

//////////////// C++ CODE FileUpload - BEGIN//////////////////////////////

 

// HelloCurl.cpp : Defines the entry point for the console application.

//

 

#include <stdio.h>

#include <curl/curl.h>

#include <iostream>

#include <fstream>

#include <string>

 

using namespace std;

 

string contents;

ofstream
strmLoginResponse("E:\\cURL\\curl-7.19.0\\src\\DLL-Debug\\Project\\HelloCurl
\\GKLoginResponse.yxt");

 

size_t WriteMemoryCallbackStdString(void *ptr, size_t size, size_t

nmemb, void *data)

{

    register int realsize = size * nmemb;

    contents = *(string *)(data);

    contents.append((char *)ptr, realsize);

 

            if (strmLoginResponse.is_open())

            {

               strmLoginResponse << contents.c_str();

            }

 

            cout << " WriteMemoryCallBackStdString : " << contents.c_str()
<< endl;

            strmLoginResponse.close();

    return realsize;

}

 

 

int main(void)

{

  CURL *curl;

  CURLcode res;

 

  string strCookiesFile =
"E:\\cURL\\curl-7.19.0\\src\\DLL-Debug\\Project\\HelloCurl\\Cookies.txt";

  string strUserAgent = "PWCAutoUploader 0.1";

 

  curl = curl_easy_init();

 

  if (curl)

  {

    /////// WebECTM Login - BEGIN//////////////////////////////////////

            curl_easy_setopt(curl, CURLOPT_COOKIEJAR,
strCookiesFile.c_str());

            curl_easy_setopt(curl, CURLOPT_USERAGENT, strUserAgent.c_str());

            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
WriteMemoryCallbackStdString);

            // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);

    // curl_easy_setopt(curl, CURLOPT_WRITEDATA, fpLoginResponse);

    curl_easy_setopt(curl, CURLOPT_URL,
"http://utcit-190:9999/webectm/logon.do");

    /* Now specify the POST data */

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
"userName=a&password=a&portalUser=N&serviceCompanyId=-1");

            curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

 

    /* Perform the request, res will get the return code */

    res = curl_easy_perform(curl);

 

    /////// WebECTM Login - END //////////////////////////////////////

 

            /// UpLoad BEGIN /////

 

    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, strCookiesFile.c_str());

            curl_easy_setopt(curl, CURLOPT_USERAGENT, strUserAgent.c_str());

            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
WriteMemoryCallbackStdString);

    curl_easy_setopt(curl, CURLOPT_URL,
"http://utcit-190:9999/webectm/loadEngineFileToDB.do");

                        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

 

            struct curl_httppost *post=NULL;

            struct curl_httppost *last=NULL;

 

            curl_formadd(&post, &last,

                                                CURLFORM_COPYNAME, "type",

                                                CURLFORM_COPYCONTENTS,
"GBSStandardFile", CURLFORM_END);

 

            curl_formadd(&post, &last,

                                                CURLFORM_COPYNAME,
"theFile",

                                                CURLFORM_FILECONTENT,
"GK-Nov-05-1-D004000_.SSS", CURLFORM_END);

            /* Set the form info */

            curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);

            curl_easy_perform(curl); /* post away! */

            /* free the post data again */

            curl_formfree(post);

 

    curl_easy_cleanup(curl);

            /// UpLoad END /////

 

  }

  return 0;

 

}

 

//////////////// C++ CODE FileUpload - END //////////////////////////////

 

Following is the verbose comparision for a successful upload from command
line against from FMS application :

 

 

 

################ SUCCESS FULL TRANSFER FROM COMMAND LINE - BEGIN
################

 

E:\cURL\curl-7.19.0\src\DLL-Debug>curl -v -b cookies.txt -A "PWCAutoUploader
0.1" -o requestOutput/uploadResult.html -F

* About to connect() to utcit-190 port 9999 (#0)

* Trying 172.19.197.207... connected

* Connected to utcit-190 (172.19.197.207) port 9999 (#0)

> POST /webectm/loadEngineFileToDB.do HTTP/1.1

> User-Agent: PWCAutoUploader 0.1

> Host: utcit-190:9999

> Accept: */*

> Content-Length: 2717

> Expect: 100-continue

> Content-Type: multipart/form-data;
boundary=----------------------------d4dc00dd636f

>

< HTTP/1.1 100 Continue

< Server: Oracle Application Server Containers for J2EE 10g (9.0.4.0.0)

< Date: Wed, 05 Nov 2008 16:18:12 GMT

  % Total % Received % Xferd Average Speed Time Time Time
Current

                                 Dload Upload Total Spent Left
Speed

  0 2717 0 0 0 0 0 0 --:--:-- --:--:-- --:--:--
0} [data not shown]

100 2717 0 0 100 2717 0 1347 0:00:02 0:00:02 --:--:--
1347< HTTP/1.1 500 Internal Server Error

< Date: Wed, 05 Nov 2008 16:18:14 GMT

< Server: Oracle Application Server Containers for J2EE 10g (9.0.4.0.0)

< Content-Location: http://utcit-190:9999/webectm/dev/jspError.jsp

< Content-Length: 3846

* Added cookie JSESSIONID="ac13c5cf270fff04733e40624128a582e6c390dfb1d6" for
domain utcit-190, path /webectm, expire 0

< Set-Cookie: JSESSIONID=ac13c5cf270fff04733e40624128a582e6c390dfb1d6;
Path=/webectm

< Cache-Control: private

< Connection: Close

< Content-Type: text/html

<

{ [data not shown]

100 6563 100 3846 100 2717 1796 1269 0:00:02 0:00:02 --:--:--
1796* Closing connection #0

 

################ SUCCESS FULL TRANSFER FROM COMMAND LINE - END
################

 

################ UNSUCCESS FULL TRANSFER FROM FMS APPLICATION - BEGIN
#########

 

* About to connect() to utcit-190 port 9999 (#0)

* Trying 172.19.197.207... * connected

* Connected to utcit-190 (172.19.197.207) port 9999 (#0)

> POST /webectm/loadEngineFileToDB.do HTTP/1.1

User-Agent: PWCAutoUploader 0.1

Host: utcit-190:9999

Accept: */*

Cookie: JSESSIONID=ac13c5cf270f973c439f142645df8997a2828f51baf5

Content-Length: 2640

Expect: 100-continue

Content-Type: multipart/form-data;
boundary=----------------------------b04d18fb2763

 

< HTTP/1.1 100 Continue

< Server: Oracle Application Server Containers for J2EE 10g (9.0.4.0.0)

< Date: Wed, 05 Nov 2008 16:11:10 GMT

< HTTP/1.1 500 Internal Server Error

< Date: Wed, 05 Nov 2008 16:11:10 GMT

< Server: Oracle Application Server Containers for J2EE 10g (9.0.4.0.0)

< Content-Length: 2418

< Connection: Close

< Content-Type: text/html

<

 WriteMemoryCallBackStdString : <HTML><HEAD><TITLE>500 Internal Server
Error</TITLE></HEAD><BODY><H1>500 Internal Server

<br> at com.evermind[Oracle Application Server Containers for J2EE 10g
(9.0.4.0.0)].server.http.ServletRequestDispatc

<br> at com.evermind[Oracle Application Server Containers for J2EE 10g
(9.0.4.0.0)].server.http.ServletRequestDispatc

<br> at com.evermind[Oracle Application Server Containers for J2EE 10g
(9.0.4.0.0)].server.http.HttpRequestHandler.pr

<br> at com.evermind[Oracle Application Server Containers for J2EE 10g
(9.0.4.0.0)].server.http.HttpRequestHandler.ru

<br> at com.evermind[Oracle Application Server Containers for J2EE 10g
(9.0.4.0.0)].server.http.HttpRequestHandler.ru

<br> at com.evermind[Oracle Application Server Containers for J2EE 10g
(9.0.4.0.0)].util.ReleasableResourcePooledExec

<br></PRE></BODY></HTML>ead.run(Thread.java:595)or.java:192)

* Closing connection #0

Press any key to continue

################ UNSUCCESS FULL TRANSFER FROM FMS APPLICATION - END
################

 

-------------------------------------------------------------------
List admin: http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-users
FAQ: http://curl.haxx.se/docs/faq.html
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2008-11-05