Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Correctly sending post requests to PHP  (Read 3761 times)

0 Members and 1 Guest are viewing this topic.

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Correctly sending post requests to PHP
« on: March 09, 2013, 06:13:46 pm »
I'm using SFML Network to access my web server and send it high scores. The high scores are recieved by a PHP script that stores them on a JSON file:

    ...
    $name = $_POST["n"];
    $validator = $_POST["v"];
    $score = $_POST["s"];
    ...

I'm currently sending a POST request from C++ like this:

    ...
    Http http; http.setHost("http://mywebsite.com");
    Request request("script.php", Http::Request::Post);
    request.setBody("n=" + mName + "&v=" + mValidator + "&s=" + toStr(mScore));
    http.sendRequest(request);
    ...

This works, but has the same limitations as a GET request - especially character limit. Since I need to validate the high scores for custom levels made in LUA, I wanted to pass the whole LUA file (stripped of symbols and whitespace) as mValidator. But it doesn't work with 1000 characters. Small strings work properly.

I've been told that character limit is only present in GET requests, and that I'm calling the POST request in the wrong way.

How can I call the POST request correctly and avoid the character limit without compressing my parameters?

JoshuaBehrens

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Correctly sending post requests to PHP
« Reply #1 on: March 09, 2013, 06:23:14 pm »
maybe the LuaScript interrupts the bodypattern. I think the params must be urlencoded although it's post.

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Re: Correctly sending post requests to PHP
« Reply #2 on: March 09, 2013, 06:39:29 pm »
maybe the LuaScript interrupts the bodypattern. I think the params must be urlencoded although it's post.

I stripped the string of all symbols and whitespace. It works for shorter strings taken by the same lua file.
How do I urlencode the string? Also, am I setting the body correctly (it looks like a GET request)?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Correctly sending post requests to PHP
« Reply #3 on: March 09, 2013, 09:11:16 pm »
By default, POST requests use the "application/x-www-form-urlencoded" MIME type, which means that they must be indeed encoded the same way as URLs. But you can change it to whatever you want (JSON, plain text, or whatever).

I don't know about such a character limit, I think it should work with any size.
Laurent Gomila - SFML developer

JoshuaBehrens

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Correctly sending post requests to PHP
« Reply #4 on: March 09, 2013, 10:21:19 pm »
You urlencode a string using this small function:
std::string urlencode( const std::string& to )
{
    std::string ret( "" );
    for ( std::size_t c( 0 ) ; c < to.length( ) ; ++c )
    {
        if ( std::isalnum( to[ c ] ) )
            ret += to[ c ];
        else
        {
            char c4[4];
            sprintf( c4, "%%%02X", to[ c ] );
            ret += c4;
        }
    }
    return ret;
}
I use it for this purpose.