1

I am trying to communicate to a php server from my gwt project.

I already got a GET request to work, however, my POST request doesn't work so far.

Here's my code:

Button synchronize = new Button("synchronize ",
                new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        String myurl = URL
                                .encode("php/test.php");

                        RequestBuilder builder = new RequestBuilder(
                                RequestBuilder.POST, myurl);

                        JSONObject jsonValue = new JSONObject();
                        jsonValue.put("name", new JSONString("Abc"));
                        builder.setHeader("Content-Type", "application/json");

                        try {
                            Request request = builder.sendRequest(jsonValue.toString(),
                                    new RequestCallback() {
                                        public void onError(Request request,
                                                Throwable exception) {
                                            processResponse("ERROR");
                                        }

                                        public void onResponseReceived(
                                                Request request,
                                                Response response) {
                                            if (200 == response.getStatusCode()) {

                                                processResponse(response
                                                        .getText());
                                            } else {
                                                processResponse("ERROR");
                                            }
                                        }
                                    });

                        } catch (RequestException e) {
                            processResponse("ERROR");
                        }

                    }
                });

public void processResponse(String responseString) {
    Window.alert(responseString);
}

I can see that the post request goes out and the request payload is a json-object. However, when I try to access the data in the php script, I get the error that the index name is undefined.

Here's the PHP:

<?php
echo $_POST["name"];
?>

Is there something wrong with my PHP?

Does anyone have a working example for this?

user3813234
  • 1,580
  • 1
  • 29
  • 44
  • Possible duplicate of [How do I send a POST request with PHP?](http://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php) – gpinkas Dec 18 '15 at 14:00
  • @gpinkas No, it is the other way round. He wants to consume a POST request from GWT in PHP. – thst Dec 18 '15 at 17:20

1 Answers1

1

While I haven't checked the PHP documentation so far, I tend to remember, that $POST contains the post request's variables, especially useful in a x-www-form-urlencoded request. .. Checked it, yes. I am right :-)

What you actually want is to read the body of the post request and parse it's JSON content to a PHP array or hash.

To read the body see here: How to get body of a POST in php?

$entityBody = file_get_contents('php://input');

Parsing json is described here: Parsing JSON file with PHP

I will not quote the code from there, as it maybe does not exactly fit your needs, but you look for json_decode($json, TRUE).

Community
  • 1
  • 1
thst
  • 4,592
  • 1
  • 26
  • 40
  • Thanks a lot, it worked now! Could you elaborate a little more about the difference between $_POST and php://input? I am used to submitting an html form to a php script and then using $_POST["nameOfField"] to access the form data. Why cant I use that here? Because I am not using a form? – user3813234 Dec 18 '15 at 18:02
  • 1
    exactly. you post data as json. this is not a form. thus the cgi rules do not apply and you need to parse the body of the post message yourself. You may want to read about CGI (very old standard) and the encoding, parsing of values in forms. Also look at the format of a POST request. basically, you upload a json file to the server, you dont send form data. – thst Dec 18 '15 at 20:17