HTTP Post

nsidney

Member
Licensed User
I'm trying to post variables to a PHP script that repeats back requested information, but I'm having trouble getting it to actually display. Here's what I have:

B4X:
Sub loginButton_Click
   Request.New1("http://tixscan.net/api/login.php")
   Request.Method = "POST"
   Request.Timeout = 30000
   Request.ContentType = "application/x-www-form-urlencoded"
   bin.New1(Request.GetStream,true) 'Use a BinaryFile object to write the data to the Request stream.
   bin.WriteString("api=1")
   Response.New1
   Response.Value = Request.GetResponse
   result = Response.GetString
   Response.Close
   Msgbox(result)
End Sub

Everything compiles right and runs, but when the message box pops up, it's just blank. It should display a zero (0) in it.

Any ideas?
 

nsidney

Member
Licensed User
Yes. I have another API that returns "NO" if the value sent is not found, and when I use the URL for that one Basic4ppc always displays "NO" even though I know the API I am sending is correct. (These APIs work in my other apps, but not in Basic4ppc). I think there is a problem with how it is sending the "api=1" string (1 is the API key for my test account).

Thanks
 

willisgt

Active Member
Licensed User
I had a similar problem about six months ago... try the same code as GET instead of POST and see what it returns.

Gary
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
BinaryFile.WriteString adds the string length as the message prefix and therefore should not be used here.
You should use BinaryFile.WriteBytes:

Bit is a Bitwise object.
B4X:
Sub Globals
 Dim Buffer(0) as Byte
End Sub

Sub loginButton_Click
    Bit.New1
    Request.New1("http://tixscan.net/api/login.php")
    Request.Method = "POST"
    Request.Timeout = 30000
    Request.ContentType = "application/x-www-form-urlencoded"
    bin.New1(Request.GetStream,true) 'Use a BinaryFile object to write the data to the Request stream.
    [B]s = "api=1"
    buffer() = bit.StringToBytes(s,0,StrLength(s))
    bin.WriteBytes(buffer())[/B]
    Response.New1
    Response.Value = Request.GetResponse
    result = Response.GetString
    Response.Close
    Msgbox(result)
End Sub
 
Top