Curl HTTP 417 – Expectation Failed

Curl HTTP 417 – Expectation Failed

WordPress XML RPC – HTTP/1.1 417 Expectation Failed

I was nocking up a client script to drag down a news-feed and post it to a WordPress blog; the script ran fine, no posts appeared and no errors occurred… irritating.

So I figured I’d share this pain in the arse issue fix to the “HTTP/1.1 417 Expectation Failed Error”

HTTP/1.1 417 Expectation Failed

$title="Test";
$body="<b>Test</b>";
$rpcurl="http://domain.tld/xmlrpc.php";
$username="";
$password="";
$category="test";
$keywords="test";

$encoding='UTF-8';
$title = htmlentities($title,ENT_NOQUOTES,$encoding); 
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding); 

$content = array( 
    'title'=>$title, 
    'description'=>$body, 
    'mt_allow_comments'=>0,  // 1 pour autoriser les commentaires
    'mt_allow_pings'=>0,  // 1 pour autoriser les trackbacks
    'post_type'=>'post', 
    'mt_keywords'=>$keywords, 
    'categories'=>array($category) 
); 
$params = array(0,$username,$password,$content,true); 
$request = xmlrpc_encode_request('metaWeblog.newPost',$params); 
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, '1');  // DEBUG OUTPUT - Turn this off for your live script (comment out)
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //Fixes the HTTP/1.1 417 Expectation Failed Bug
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); 
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, false); 
$results = curl_exec($ch);
curl_close($ch); 
echo $results;

Or as a function

function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8')
{
    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

    $content = array(
        'title'=>$title,
        'description'=>$body,
        'mt_allow_comments'=>0,  // 1 to allow comments
        'mt_allow_pings'=>0,  // 1 to allow trackbacks
        'post_type'=>'post',
        'mt_keywords'=>$keywords,
        'categories'=>array($category)
    );

    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //Fixes the HTTP/1.1 417 Expectation Failed Bug
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);

    $results = curl_exec($ch);
    curl_close($ch);

    return $results;
}

Just to expand, "checkupdown" defines the error as follows

Your Web server thinks that the HTTP data stream sent by the client (e.g. CURL) contains a ‘Expect’ request which can not be satisfied. The Expect request is fairly generic i.e. loosely defined in the HTTP protocol. It can specify more than one expectation, each of which may be interpreted differently by different Web servers.

CUD: http://www.checkupdown.com/status/E417.html

Hopefully that will save someone banging their frickin skull on the wall

Curl HTTP 417 – Expectation Failed in C#

System.Net.HttpWebRequest adds the header 'HTTP header "Expect: 100-Continue"' to each request unless you explicitly tell it not to by setting this static property to false:

System.Net.ServicePointManager.Expect100Continue = false;

you can also set that in web.config

<system.net>
  <settings>
     <servicePointManager expect100Continue="false"/>

If you encounter errors from this change ensure HttpWebRequest's KeepAlive is set to false and obviously


References

Chris McKee

Chris McKee

https://chrismckee.co.uk

Software Engineer, Web Front/Backend/Architecture; all-round tech obsessed geek. I hate unnecessary optimism