简体   繁体   中英

httpful POST request: get raw HTTP message

I use httpful to post a message:

try
{
    $ret = \Httpful\Request::post($url)
            ->addHeaders(array(
                'Content-Type' => 'application/x-www-form-urlencoded'
            ))
            ->body("mystring=".urlencode($msg))
            ->send();
} catch (Exception $e)
{
    trigger_error("Exception :".$e->getMessage()."\n");
}

The system on the other side cannot read it and also cannot put out debug information - how can you get the raw HTTP message (including headers, separators, etc.) that is actually sent?

try {
    $url = 'https://httpbin.org/post1'; // hasErrors
//    $url = 'https://httpbin.org1/post'; // ConnectionErrorException
    $msg = 'qwe';
    $req = \Httpful\Request::post($url)
                           ->addHeaders([
                               'Content-Type' => 'application/x-www-form-urlencoded',
                           ])
                           ->body('mystring=' . urlencode($msg));
    $res = $req->send();

    if ($res->hasErrors()) {
        print_r("
            <pre>
            *** REQUEST ***             
            Headers: {$req->raw_headers}
            Body: {$req->payload}
            
            
            *** RESPONSE ***  
            Headers: {$res->raw_headers}
            Body: <xmp>{$res->raw_body}</xmp>
            </pre>           
        ");
    }

} catch (\Httpful\Exception\ConnectionErrorException $e) {
    if (isset($req)) {
        print_r("
            <pre>
            *** REQUEST ***             
            Headers: {$req->raw_headers}
            Body: {$req->payload}                                 
            </pre>           
        ");
    }
    if (isset($res)) {
        print_r("
            <pre>
            *** RESPONSE ***  
            Headers: {$res->raw_headers}
            Body: <xmp>{$res->raw_body}</xmp>
            </pre>
        ");
    }
}

To get what was actually sent, I feel that the most reliable way is to access the data which has actually been sent, ie, after it has been sent. Otherwise you'll never be really sure.

To do this, you can intercept the HTTP stream or, to make things easier, you can trick the server into resolving the URL to what is actually a proxy. With the appropriate certificates or instruction to trust same, you can get access to the whole request as well as the whole response, plus timings, raw data, the works.

I usually employ a custom HTTPS MitM module to do this, but there are several alternatives (just google "logging https proxy") - one I just found is MockServer , which seems to support several scenarios.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM