Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST method doesn't working #14

Closed
miladr opened this issue Dec 3, 2014 · 2 comments
Closed

POST method doesn't working #14

miladr opened this issue Dec 3, 2014 · 2 comments

Comments

@miladr
Copy link

miladr commented Dec 3, 2014

I'm using this code to send a POST request but it's not working.

<?php

require 'vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);

$factory = new React\HttpClient\Factory();
$client = $factory->create($loop, $dnsResolver);

$request = $client->request('POST', 'http://respondto.it/bomber');

$request->write("test=12345");

$request->on(
    'response',
    function ($response) {
        echo "recived".PHP_EOL;
        $response->on(
            'data',
            function ($data) {
                var_dump($data);
            }
        );
    }
);
$request->end();
$loop->run();

Result is

POST /bomber
Headers

Connect-Time: 1
Connection: close
Host: respondto.it
Total-Route-Time: 0
User-Agent: React/alpha
Version: HTTP/1.1
Via: 1.1 vegur
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Request-Id: 6596bd3e-1505-45a9-adbd-a0a5f4c7ea52

I know that i'm doing something wrong but i couldn't find solution.

@clue
Copy link
Member

clue commented Dec 3, 2014

Would you care to elaborate on what "but it's not working" refers to? :)

Sending HTTP requests which include a body (such as your POST) requires the client to tell the server the expected length either via the Content-Length header or via chunked transfer encoding (which is currently not supported). Also, the data you're sending appears to be formatted like HTML form data, so you need to specify the correct Content-Type header like so:

$data = 'test=12345';
$request = $client->request(
    'POST',
    'http://respondto.it/bomber',
    array(
        'Content-Length' => strlen($data),
        'Content-Type' => 'application/x-www-form-urlencoded'
    )
);
$request->end($data);

@miladr
Copy link
Author

miladr commented Dec 3, 2014

As you can see in result, body wasn't received.
But it was my mistake. I forgot to add headers because i expected that Client will add necessary Headers. I suggest to add this to documentation.
Thank you.

@miladr miladr closed this as completed Dec 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants