Question

I'm trying to construct an HTTP form using libcurl but I can't get it to work properly. Every time I call curl_formadd it returns CURL_FORMADD_OPTION_TWICE. The only information about this error indicates that libcurl thinks I'm trying to add two form elements with the same name, even though its the first call to curl_formadd and I'm only adding one element!

  Declare Function curl_global_init Lib "libcurl" (flags As Integer) As Integer
  Declare Function curl_formadd Lib "libcurl" (FirstItem As Ptr, LastItem As Ptr, Option1 As Integer, Value1 As Ptr, Option2 As Integer, Value2 As Ptr, EndMarker As Integer) As Integer

  Const CURLFORM_COPYCONTENTS = 2
  Const CURLFORM_COPYNAME = 1
  Const CURLFORM_END = 17

  Dim formname, formvalue As MemoryBlock
  formname = "NAME"
  formvalue = "CONTENTS"

  If curl_global_init(3) = 0 Then
    Dim first, last As Ptr
    Dim err As Integer
    err = curl_formadd(first, last, CURLFORM_COPYNAME, formname, CURLFORM_COPYCONTENTS, formvalue, CURLFORM_END)
    Break
    ' err is 2 (CURL_FORMADD_OPTION_TWICE) 
  End If

What is this error trying to tell me?

Was it helpful?

Solution

Reading the 'man' page for curl_formadd(), it says there:

The pointers firstitem and lastitem should both be pointing to NULL in the first call to this function.

You, however, pass NULL for these.

You also seem to be passing the strings incorrectly. Try defining the Value1 and Value2 parameters "as CString", then pass normal Strings, not Memoryblocks.

Lastly, you gave CURLFORM_COPYCONTENTS the wrong code. It's not 2 but 4. See the CURLformoption enum in curl.h: "CFINIT(NOTHING)" gets value 0, and every item past that gets one higher, so CFINIT(COPYCONTENTS) gets 4.

Here's the code that works for me:

Declare Function curl_global_init Lib "libcurl" (flags As Integer) As Integer
Declare Function curl_formadd Lib "libcurl" (ByRef FirstItem As Ptr, ByRef LastItem As Ptr, Option1 As Integer, Value1 As CString, Option2 As Integer, Value2 As CString, EndMarker As Integer) As Integer

Const CURLFORM_COPYCONTENTS = 4
Const CURLFORM_COPYNAME = 1
Const CURLFORM_END = 17
const CURL_GLOBAL_ALL = 3

Dim formname, formvalue As String
formname = "NAME"
formvalue = "CONTENTS"

If curl_global_init(CURL_GLOBAL_ALL) = 0 Then
  Dim first, last As Ptr
  Dim err As Integer
  err = curl_formadd(first, last, CURLFORM_COPYNAME, formname, CURLFORM_COPYCONTENTS, formvalue, CURLFORM_END)
  Break
  ' err is 0
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top