Archived Forum Post

Index of archived forum posts

Question:

Upload with ChilkatAX 9.5.0.51: CGI Params not set

Jul 23 '15 at 01:19

Hi!

I am trying to do a https upload of a file to a CGI (https with basic http auth). I can do that successfully from a browser using the following form:

<form action="https://www.server.com/cgi/thisistheuploadcgiscript.pl" method="post" 
enctype="multipart/form-data">
<input type="file" name="name" accept="text/*" maxlength="2097152">
<input type="submit">
</form>

I therefore assume that my CGI script is working properly.

When I try uploading the very same file using ChilkatActiveX in VB6 (I do have a Chilkat license) it generally seems to work but the "name" CGI parameter given in the AddFileForUpload Method doesn't seem to be set, at least it is not available when reading the CGI params.

Below is the code I use where the CGI param "name" should be "999997" and it is "".

Setting the "name" to "999997" in .AddFileForUpload is no mystery. I just don't get it.

I just found that adding additional parameters using .AddParam Key,Value does not work for me either. Maybe that's the heart of my problem.

Help will be appreciated!

T.

Private Sub UploadFile()
' based on example code from http://www.example-code.com/vb/vb6_http_post_progress.asp

Dim Hostname As String
Dim Success As Long

Hostname = "www.myhostname.com"

Set UploadHttp = New ChilkatHttp

Success = UploadHttp.UnlockComponent(ChilkatHttpLicense)
If (Success <> 1) Then
    WriteDebugLog ("WebDownload unlock Error: " & vbCrLf & UploadHttp.LastErrorText)
    Exit Sub
End If

UploadHttp.ConnectTimeout = 20
UploadHttp.FetchFromCache = 0

If Len(ProxyURL) > 0 Then
    UploadHttp.ProxyDomain = ProxyURL
    UploadHttp.ProxyPort = ProxyPort
    If Len(ProxyUsername) > 0 Then
        UploadHttp.ProxyLogin = ProxyUsername
        UploadHttp.ProxyPassword = ProxyPassword
    End If
End If

Set Cert = UploadHttp.GetServerSslCert(Hostname, 443)

If Not verifyWebCert(Hostname, Cert.Sha1Thumbprint) Then
    Exit Sub
End If

UploadHttp.login = "TheUsername"
UploadHttp.Password = "ThePassword"

UploadHttp.HeartbeatMs = 100

Dim req As New ChilkatHttpRequest
' req.UseUpload
req.HttpVerb = "PUT"
req.ContentType = "multipart/form-data"

req.AddFileForUpload "999997", "C:\This\is\a\valid\path\to\the\file.txt"
req.Path = "/cgi/thisistheuploadcgiscript.pl"

Dim response As ChilkatHttpResponse
Set response = UploadHttp.SynchronousRequest(Hostname, 443, True, req)

WriteDebugLog UploadHttp.LastErrorText

End Sub

' The AbortCheck event is called periodically according to the
' HeartbeatMs property setting.
Private Sub UploadHttp_AbortCheck(abort As Long)

    If (ProgressBar1.Value > 95) Then
        ProgressBar1.Value = 0
    Else
        ProgressBar1.Value = ProgressBar1.Value + 5
    End If

End Sub

' This event fires just before beginning to receive the HTTP response.
Private Sub UploadHttp_BeginReceive()
    WriteDebugLog "Begin Receive"
End Sub

' This event fires just before beginning to send the HTTP upload.
Private Sub UploadHttp_BeginSend()
    WriteDebugLog "Begin Send"
End Sub

' This event fires just after the HTTP response has been received in full.
' (or after a failure in reading the response).  If reading the response
' failed, success is set to 0.  Otherwise it is set to 1.
Private Sub UploadHttp_EndReceive(ByVal Success As Long)
    WriteDebugLog "End Receive"
    CloseProgress
    If Success = 0 Then MsgBox "upload failed"
End Sub

' This event fires just after the HTTP upload has been sent.
' (or after a failure in sending the upload).  If sending the upload
' failed, success is set to 0.  Otherwise it is set to 1.
Private Sub UploadHttp_EndSend(ByVal Success As Long)
    WriteDebugLog "End Send"
    CloseProgress
    If Success = 0 Then MsgBox "upload failed" 
End Sub

' The pctDone argument will contain a value from 1 to 100.

Private Sub UploadHttp_PercentDone(ByVal percent As Long, abort As Long)
    ProgressBar1.Value = percent
End Sub

LastErrorTXT

ChilkatLog:
  SynchronousRequest:
    DllDate: Jun 23 2015
    ChilkatVersion: 9.5.0.51
    UnlockPrefix: 4SSDEkHttp
    Username: UserName
    Architecture: Little Endian; 32-bit
    Language: ActiveX
    VerboseLogging: 0
    domain: www.myhostname.com
    port: 443
    ssl: 1
    httpRequest:
      httpVersion: 1.1
      verb: PUT
      path: /cgi/thisistheuploadcgiscript.pl
      contentType: multipart/form-data
      charset: windows-1252
      sendCharset: 0
      mimeHeader: 
      requestParams:
        requestItem:
          name: 999997
          fileOnDisk: C:\This\is\a\valid\path\to\the\file.txt
          numValueBytes: 52
        --requestItem
        requestItem:
          name: customer_id
          value: 999997
        --requestItem
      --requestParams
    --httpRequest
    readTimeout: 20
    connectTimeout: 20
    approxRequestSize: 228
    fullRequest:
      a_synchronousRequest:
        generateRequest:
          httpRequestGenStartLine:
            genStartLine:
              startLine: PUT /cgi/thisistheuploadcgiscript.pl HTTP/1.1
            --genStartLine
          --httpRequestGenStartLine
          addCookies:
            Not auto-adding cookies.
            sendCookies: 1
            cookieDir: 
          --addCookies
          genMultipartFormData:
            requestParam:
              name: 999997
              filename: C:\This\is\a\valid\path\to\the\file.txt
            --requestParam
            requestParam:
              name: customer_id
            --requestParam
          --genMultipartFormData
        --generateRequest
        fullHttpRequest:
          domain: www.myhostname.com
          port: 443
          ssl: 1
          openHttpConnection:
            Opening connection directly to HTTP server.
            httpHostname: www.myhostname.com
            httpPort: 443
            ssl: 1
            HTTPS secure channel established.
          --openHttpConnection
          connectTime: Elapsed time: 313 millisec
          sendRequestHeader:
            sendHeaderElapsedMs: 0
          --sendRequestHeader
          sendRequestBody:
            genMultipartFormData:
              requestParam:
                name: 999997
                filename: C:\This\is\a\valid\path\to\the\file.txt
              --requestParam
              requestParam:
                name: customer_id
              --requestParam
            --genMultipartFormData
            sendBodyElapsedMs: 0
          --sendRequestBody
          statusCode: 401
          statusText: Unauthorized
          readResponseBody:
            contentLength: 364
          --readResponseBody
          checkCloseConnection:
            Response includes connection:close header (or proxy-connection:close header)
          --checkCloseConnection
        --fullHttpRequest
        generateRequest:
          httpRequestGenStartLine:
            genStartLine:
              startLine: PUT /cgi/thisistheuploadcgiscript.pl HTTP/1.1
            --genStartLine
          --httpRequestGenStartLine
          addCookies:
            Not auto-adding cookies.
            sendCookies: 1
            cookieDir: 
          --addCookies
          genMultipartFormData:
            requestParam:
              name: 999997
              filename: C:\This\is\a\valid\path\to\the\file.txt
            --requestParam
            requestParam:
              name: customer_id
            --requestParam
          --genMultipartFormData
        --generateRequest
        fullHttpRequest:
          domain: www.myhostname.com
          port: 443
          ssl: 1
          openHttpConnection:
            Opening connection directly to HTTP server.
            httpHostname: www.myhostname.com
            httpPort: 443
            ssl: 1
            HTTPS secure channel established.
          --openHttpConnection
          connectTime: Elapsed time: 312 millisec
          sendRequestHeader:
            sendHeaderElapsedMs: 0
          --sendRequestHeader
          sendRequestBody:
            genMultipartFormData:
              requestParam:
                name: 999997
                filename: C:\This\is\a\valid\path\to\the\file.txt
              --requestParam
              requestParam:
                name: customer_id
              --requestParam
            --genMultipartFormData
            sendBodyElapsedMs: 0
          --sendRequestBody
          statusCode: 200
          statusText: OK
          readResponseBody:
            Response is chunked.
          --readResponseBody
        --fullHttpRequest
        success: 1
      --a_synchronousRequest
      success: 1
    --fullRequest
    totalTime: Elapsed time: 1125 millisec
    Success.
  --SynchronousRequest
--ChilkatLog

In perl cgi I read the params like this:

my $query = new CGI;
my $filename = $query->param("name");
my $cust = $query->param("customer_id");
Like said: Using a browser I get values, using ChilkatHttp, these Params are empty ""


Answer

When you test with the browser, using the HTML form as shown above, you are sending a POST where the HTTP request is multipart/form-data.

In your code using Chilkat, you're setting the request.HttpVerb = "PUT". You should be using a "POST". The remainder of your code seems OK. You can examine the exact HTTP request sent by Chilkat by setting the Http.SessionLogFilename property so that the exact HTTP request is logged to a file. See http://stackoverflow.com/questions/4238809/example-of-multipart-form-data for an example of a multipart/form-data request.


Answer

Thanks a lot! That was it... I feel so silly that in the end it was a simple typo...