B4R Question [rHttpUtils2] POST request does not work

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Sending the data through the POST request does not work
B4X:
Sub JobDone (Job As JobResult)
    Log("*******************************")
    Log("JobName: ", Job.JobName)
    If Job.Success Then
        Dim bc As ByteConverter
        Log("Response: ", bc.SubString2(Job.Response, 0, Min(200, Job.Response.Length)))
        If Job.JobName = "Example" Then
            Dim buffer(300) As Byte
            Dim raf As RandomAccessFile
            raf.Initialize(buffer, True)
            WriteBytes(raf, "{""id"":""61751246""")
            WriteBytes(raf, "}")
            HttpJob.Initialize("Example2")
'            Dim Datos As String = JoinStrings (Array As String ("{""id"":""",61751246,"""}"))
            Log("Datos: ",bc.SubString2(buffer, 0, raf.CurrentPosition))
'            HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
            HttpJob.AddHeader("Content-Type", "application/json")
            Log("stack: ", StackBufferUsage, ", buffer size:", raf.CurrentPosition)
            HttpJob.Post("https://chatbot-dot-central-dot-red-amarilla.appspot.com/_ah/api/consultarServicioXidServicioUsuario", bc.SubString2(buffer, 0, raf.CurrentPosition))
'            HttpJob.Post("https://www.b4x.com/print.php?key1=value1", "PostKey1=PostValue2&abc=def")
        End If
    Else
        Log("ErrorMessage: ", Job.ErrorMessage)
        Log("Status: ", Job.Status)
        Log(Job.Response)
    End If
End Sub

Private Sub WriteBytes(raf As RandomAccessFile, Data() As Byte)
    raf.WriteBytes(Data, 0, Data.Length, raf.CurrentPosition)
End Sub

ErrorMessage: Failed to connect
Log: Status: 0
 

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Even this example does not work, which at some time served me no longer
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've just tested this program on ESP8266:
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 2000
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    wifi.ConnectAsync("xxx", "xxx", 0, Null, "wifi_Connected")
End Sub


Sub WiFi_Connected (Success As Boolean)
    Log("connected: ", Success)
    If Success Then
        HttpJob.Initialize("Example")
        HttpJob.Download("https://www.example.com")
    End If
End Sub


Sub JobDone (Job As JobResult)
    Log("*******************************")
    Log("JobName: ", Job.JobName)
    If Job.Success Then
        Dim bc As ByteConverter
        Log("Response: ", bc.SubString2(Job.Response, 0, Min(200, Job.Response.Length))) 'truncate to 200 characters
        If Job.JobName = "Example" Then
            'send another request
            'This time it is a POST request and we set the Content-Type header
            HttpJob.Initialize("Example2")
            'add headers before calling Post or Download (this is different than the standard HttpUtils2 library).
            HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
            HttpJob.Post("http://www.b4x.com/print.php?key1=value1", "PostKey1=PostValue2&abc=def")
        End If
    Else
        Log("ErrorMessage: ", Job.ErrorMessage)
        Log("Status: ", Job.Status)
        Log(Job.Response)
    End If
End Sub

Works as expected. The logs ends with:


*******************************
JobName: Example2
Response: POST variables:
array(2) {
["PostKey1"]=>
string(10) "PostValue2"
["abc"]=>
string(3) "def"
}

GET variables:
array(1) {
["key1"]=>
string(6) "value1"
}
 
Upvote 0

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
My Log:

Conectado a la red!!
*******************************
JobName: Example
Response: {"Listado":["{\"Nombre\":\"Jhon Jairo\",\"Tipo\":\"Administrador\",\"Telefono\":\"2147483647\",\"Marca\":\"\",\"TipoVehiculo\":\"\",\"Apellido\":\"Caro\",\"Placa\":\"\",\"Cedula\":\"80032157\"}","{\"N
trying to connect to: www.b4x.com port: 80 ssl: 0
connected: www.b4x.com
HTTP/1.1 301 Moved Permanently
Date: Thu, 06 Jan 2022 15:51:43 GMT
Server: Apache/2.4.52 (cPanel)
OpenSSL/1.1.1m mod_bwlimited/1.4
Location: https://www.b4x.com/print.php?key1=value1
Content-Lengt
h: 249
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//
IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved P
ermanently</h1>
<p>The document has moved <a href="https://www.b4x.com/print.php?key1=value1">here</
a>.</p>
</body></html>

Redirecting to: https://www.b4x.com/print.php?key1=value1
trying to connect to: www.b4x.com port: 443 ssl: 1
*******************************
JobName: Example2
ErrorMessage: Failed to connect
Status: 0
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've now tested it on ESP32 as well. Works fine here.

Logs:

ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1100
load:0x40078000,len:9564
ho 0 tail 12 room 4
load:0x40080400,len:6320
entry 0x400806a8
AppStart
connected: 1
trying to connect to: www.example.com port: 443 ssl: 1
connected: www.example.com
HTTP/1.0 200 OK
Age: 574904
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8

Date: Fri, 07 Jan 2022 07:10:30 GMT
Etag: "3147526947+ident"
Expires: Fri, 14 Jan 2022 07:10:30 G
MT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (bsa/EB11)
Vary: Accept-Encoding
X-C
ache: HIT
Content-Length: 1256
Connection: close
<!doctype html>
<html>
<head>
<title>Examp
le Domain</title>

<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/ht
ml; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
pa
dding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans"
, "Helvetica Neue", Helvetica, Arial, sans-serif;

}
div {
width: 600px;

margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radiu
s: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {

color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {

margin: 0 auto;
width: auto;
}
}
</style>
</head>

<bod
y>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in docum
ents. You may use this
domain in literature without prior coordination or asking for permission.
</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body
>
</html>

*******************************
JobName: Example
Response: <!doctype html>
<html>
<head>
<title>Example Domain</title>

<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" conten
trying to connect to: www.b4x.com port: 80 ssl: 0
connected: www.b4x.com
HTTP/1.1 301 Moved Permanently
Date: Fri, 07 Jan 2022 07:10:31 GMT
Server: Apache/2.4.52 (cPanel)
OpenSSL/1.1.1m mod_bwlimited/1.4
Location: https://www.b4x.com/print.php?key1=value1
Content-Lengt
h: 249
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//
IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved P
ermanently</h1>
<p>The document has moved <a href="https://www.b4x.com/print.php?key1=value1">here</
a>.</p>
</body></html>

Redirecting to: https://www.b4x.com/print.php?key1=value1
trying to connect to: www.b4x.com port: 443 ssl: 1
connected: www.b4x.com
HTTP/1.1 200 OK
Date: Fri, 07 Jan 2022 07:10:34 GMT
Server: Apache/2.4.52 (cPanel) OpenSSL/1.1.1m
mod_bwlimited/1.4
Upgrade: h2,h2c
Connection: Upgrade, close
Vary: Accept-Encoding,User-Agent
Co
ntent-Type: text/html; charset=UTF-8
POST variables:
array(2) {
["PostKey1"]=>
string(10)
"PostValue2"
["abc"]=>
string(3) "def"
}

GET variables:
array(1) {
["key1"]=>
string(6)
"value1"
}

*******************************
JobName: Example2
Response: POST variables:
array(2) {
["PostKey1"]=>
string(10) "PostValue2"
["abc"]=>
string(3) "def"
}

GET variables:
array(1) {
["key1"]=>
string(6) "value1"
}

Might be some network issue.
 
Upvote 0

KiloBravo

Active Member
Licensed User
I copied Erel's code exactly, I just changed my SSID and PSW.

ESP8266 (Lolin D1 Mini Pro)
Logs .....
AppStart
connected: 0
*Reboot*
AppStart
connected: 0

ESP32 (Lolin D32 Pro)
Logs .....
AppStart
*Reboot*
OK
>AppStart
*Reboot*
AppStart
Hold *Reboot* for 5 seconds
OK
>AppStart

Hopefully that helps troubleshooting ?
 
Upvote 0

KiloBravo

Active Member
Licensed User
They only line I changed was wifi.ConnectAsync("xxx", "xxx", 0, Null, "wifi_Connected")
to ...
wifi.ConnectAsync("FiOS-XR735", "DEA2398CAR9743HIP", 0, Null, "wifi_Connected")
 
Upvote 0
Top