[Gambas-user] Class HTTPForm

T Lee Davidson t.lee.davidson at gmail.com
Tue Apr 12 00:58:00 CEST 2022


On 4/11/22 10:20, T Lee Davidson wrote:
> On 4/11/22 04:30, Hans Lehmann wrote:
>> Hello.
>>
>> In the Gambas documentation I can't find a hint for which *purpose* one uses the class HTTPForm. I don't have a look at the 
>> source code of a small project that shows how to use this class. Therefore I ask if someone can help with an example.
>>
>> With kind regards
>>
>> Hans
> 
> It's purpose is stated in the first paragraph, "This class allows to forge an HTTP request equivalent to an HTML form post." 
> Perhaps it needs to be worded differently to make it more clear?
> 
> It is simply an easier manner in which to craft and submit a HTML form than using HttpClient. As you can see, it inherits 
> HttpClient and introduces four new methods: Add, AddFile, Remove, and Submit.
> 
> I don't have time at the moment to create and test an example. But, it should be relatively simple following the HttpClient 
> examples. Just add your form data using the .Add() method [ie. HttpForm.Add("myField1", "myValue1")] as many times as needed, 
> and then submit the form with HttpForm.Submit().

Okay, here's a tested example based on the synchronous HttpClient example:

[code]
Public Sub SubmitForm()

   Dim hFormClient As HttpForm
   Dim sBuffer As String

   hFormClient = New HttpForm As "hFormClient"
   With hFormClient
     .URL = URL '[Url Of form On server]
     .Async = False
     .Timeout = 15
     .Add("login", "MyLoginName")
     .Add("password", "MySecretPassword")
     .Submit
   End With

   If hFormClient.Status < 0 Then
     Print "ERROR"
   Else
     ' Success - read the data
     If Lof(hFormClient) Then sBuffer = Read #hFormClient, Lof(hFormClient)
     Print sBuffer
   End If

End
[/code]

Posting a file using the .AddFile method should be relatively simple as well.


-- 
Lee


More information about the User mailing list