1

I saw a few posts here that have the same problem but none of them are a simple as mine and hence the post.

I am new to php and I see that the $_POST and $_REQUEST variable are both empty. I can get the headers using the apache_request_headers() and am able to see the headers properly. It's just the $_POST variable is empty.

My client is an Android app and is generating the post message properly. I used tcpdump to test it. Also apache logs show that the intended PHP is invoked. I use Apache 2.2.14 and PHP 5.2.12 the standard installation, nothing special.

Can anyone think of any reason why the $_POST variable is empty?

Also I am not using a class in my code. Its just plain code which calls functions from other classes as needed. I mention this because I saw another post that suggests to use $this->input->post() but that is not possible in my case.

EDIT: Found the problem thanks to Sean below. The "Content-Type" was not set properly and that caused the actual values not reaching the php code. Oddly enough the headers of the empty request are what I set them to. Why would the data be stripped?

8
  • Show us your dump. I suspect that you actually aren't generating the headers correctly. Commented Jul 4, 2011 at 4:04
  • 2
    Have you checked if $_GET is empty too? Commented Jul 4, 2011 at 4:05
  • @vascowhite: In that case, $_REQUEST wouldn't have been empty too. Commented Jul 4, 2011 at 4:06
  • how are you creating the HTTP POST request? Commented Jul 4, 2011 at 4:07
  • You are probably not posting variables properly, can you provide some code? Commented Jul 4, 2011 at 4:08

2 Answers 2

3

Make sure that your Android client is sending the correct content type on POST. It should be set to application/x-www-form-urlencoded.

        url = new URL("your URL");
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestMethod("POST");
Sign up to request clarification or add additional context in comments.

2 Comments

I actually ran into this same issue when I was writing an iOS application that communicated with a Microsoft ASP.NET application.
You are right! The content-type was wrong. Setting it properly makes it work.
1

First I would check that your server isn't redirecting your POST data with a 301 'moved permanently' code. Open you're log file to check this. If its working well you should see a 200 code (or 201 at worst). You'll notice that for each call to the server a POST and GET code will be generated. Make sure both are 200. If only GET is 200 but POST is 301 then your server is redirecting for some reason (e.g. you're calling http://mysite.com, but the server redirects to http://www.mysite.com).

If you need to test GET without a return value to android try something like this using HttpPost in android:

HttpPost httppost = new HttpPost("http://www.mysite.com?somevarname=somevalue");
httpclient.execute(httppost);

Then from the server side try doing something like creating a file, or something if GET vars are present.

But if your log has a redirect code for both GET and POST then you need to override the redirect with a .htaccess or configure your host to stop any redirect.

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.