B4J Question Parameter string Poststring to php not sent

Anno Jo

Member
Hi everyone..

I'm new n glad to join this great forum..
Maybe this is a little stupid question but this made me headache for days...o_O
I was able to communicate with server via php with jOkhttputils2 download2 /get, but why Post request never success?
I have
B4X:
Sub test
    Dim Job As HttpJob
    Job.Initialize("", Me)
    Try

        Job.PostString(strURL & "testpost.php","action=test")
        Job.GetRequest.SetContentType("")
'        Job.PostMultipart(strURL &  "post_dil.php",Map1,Null)
'        Job.GetRequest.SetContentType("application/json")
'        Job.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
'        Job.GetRequest.SetContentType("application/json")
'        Job.GetRequest.Timeout=60
'        Job.GetRequest.SetContentEncoding("UTF8")
        
        Wait For (Job) JobDone(Job As HttpJob)
        If Job.Success Then
            Log(Job.GetString)
        Else
            Log("error :" & Job.ErrorMessage)
            Job.Release
        End If
    Catch
        Job.Release
        Log("Error: " & LastException.Message)
'        LogError
    End Try
    
End Sub

php
PHP:
<?php
    //require 'db.php';
    try
    {
        $JsonString = file_get_contents("php://input");
        echo 'aaa ';
        echo "a ".$JsonString;


        if(isset($_POST['action'])){
            echo 'Status: ';
        }else
        {
            echo 'noooooooooooo ';
        }
 
        $JsonString = file_get_contents("php://input");
        echo 'bbb ';
        echo "bb".$JsonString;
    }
    catch (Exception $e)
    {
        print json_encode("Failed");
        echo '<br />Caught exception: '.$e->getMessage()."\n";
    } 
?>

the log
1618546162723.png


why parameter/string 'action' never sent/catch? it's just a simple parameter. but when I use GET request in other sub it works, did I miss something?

Thank a lot
 

Anno Jo

Member
Remove the Try / Catch block. It is wrong.

The content type should be correct for this to work.

Just delete line 7 or explicitly set it to:
B4X:
Job.GetRequest.SetContentType("application/x-www-form-urlencoded")

Hi @Erel
I did but still give me no luck..
the strange thing is, GET is works without any problem. and both POST & GET in android/b4a work great
 
Upvote 0

Anno Jo

Member
just test it to http://httpbin.org/post by changing:
B4X:
Job.PostString("http://httpbin.org/post","action=test&master=Erel")

log
1618554019577.png


it gave me result o_O o_O
so there must be something in my server, just couldn't find it.
but why in b4a works?

maybe should I update the Lib? my jOkHttpUtils ver 2.91
 
Upvote 0

Anno Jo

Member
deleting file_get_contents not help either..
Switch to B4J instead of PHP and everything will be simpler.
did you mean B4J server? Okay Erel, I will try it.
the reason I'm using post method is because I want to send huge json array as suggested in forum.

Thanks
Anno
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
1. You are sending a simple string "action=test" but you "must" send a JSON-formatted string.
2. Create a map and add "action" as the key and "test" as the value
3. Use the JSON-generator with the map and you get {"action":"test"}
4. In php decode the JSON to an array:

B4X:
$jsonstring = file_get_contents("php://input");
$myarray = json_decode($jsonstring, true);
$action = $myarray["action"];

...
 
Upvote 0

Anno Jo

Member
1. You are sending a simple string "action=test" but you "must" send a JSON-formatted string.
2. Create a map and add "action" as the key and "test" as the value
3. Use the JSON-generator with the map and you get {"action":"test"}
4. In php decode the JSON to an array:

B4X:
$jsonstring = file_get_contents("php://input");
$myarray = json_decode($jsonstring, true);
$action = $myarray["action"];

...

Thanks @KMatle , that was just test since I already post json string and nothing sent.
anyway, I try your suggestion but nothing's change

I try
B4X:
Dim Job As HttpJob
    Job.Initialize("", Me)
    Dim m As Map = CreateMap("action":"test","master":"Erel")
    'm.Initialize
    Dim dataJson As JSONGenerator
    dataJson.Initialize(m)
    
'    http://httpbin.org/post
    Job.PostString("http://httpbin.org/post",dataJson.ToString)
        
'    Job.PostString(strURL & "testpost.php",dataJson.ToString)
'    Job.GetRequest.SetContentType("application/x-www-form-urlencoded")
    Job.GetRequest.SetContentType("application/json")
        
    Wait For (Job) JobDone(Job As HttpJob)
    If Job.Success Then
        Log(Job.GetString)
    Else
        Log("error :" & Job.ErrorMessage)
        Job.Release
    End If

1618567086818.png

with http://httpbin.org/post I can see it was sent successfully.

but not with my "testpost.php"
PHP:
<?php
    //require 'db.php';
    try
    {
        $jsonstring = file_get_contents("php://input");
        $myarray = json_decode($jsonstring, true);
        $action = $myarray["action"];
        
        
        if (empty($action))
        {
            print json_encode("Parameter is empty");
            
        }
        else
        {
            print json_encode("Parameter is sent");
        }
        
        //if(isset($_POST['action'])){
        //    echo 'Status: ';
        //}else
        //{
        //    echo 'noooooooooooo ';
        //}
 
    }
    catch (Exception $e)
    {
        print json_encode("Failed");
        echo '<br />Caught exception: '.$e->getMessage()."\n";
    } 
?>

log give me "Parameter is empty".
could be my server config?
 
Upvote 0

Anno Jo

Member
It's really confusing. I hope someone who have same experience could give advice.
its just in B4J, B4A no problem.
I already search everything in forum at first, always..

thanks experts, I'm really new in coding n will continue learning ?
 
Upvote 0

Anno Jo

Member
1. change SetContentType
2. port
3. server config
4. update lib
5 php file

just open both b4j & b4a project. use the same php file that works with b4a project. copy the sub job from b4a to b4j (the request using PostMultipart)
run it both & see each log.

PHP:
<?php
    require 'db.php';

    if(isset($_POST['action'])) {
    }else{
    die('Access restricted');   
    }
    
    ....

the b4a run perfect catch all key and value from json data, even image also sent.
b4j get 'Access restricted' response, means no data arrived.

OkHttpUtils2 & jOkHttpUtils2 identical right? different result confused me

I almost give up. but I will back when it solved

Thanks
 
Upvote 0

Anno Jo

Member
after struggling so hard, i found that:

this work for GET but not work for POST
B4X:
dim strUrl as string = "http://www.domain.com/"

Job.PostMultipart(strUrl & "testpost.php", CreateMap("Action": "test"), Null)

this work for bot Get & Post
B4X:
Job.PostMultipart("http://www.domain.com/testpost.php", CreateMap("Action": "test"), Null)

please someone tell me why? should I do something to the link/url?

Thanks
 
Upvote 0

Anno Jo

Member
update:
it's all my mistake.. link variable strUrl minus www ?‍♂️
even though GET method was still successful without it. so after i change "http://www.domain.com/" instead of "http://domain.com/" everything's ok

these all 4 day headache just because those three letters o_O
Thank you everyone, I really feel the warmth of this community

Anno
 
Upvote 0
Top