upload_with_media responses 403 error with null response


#1

I’m trying to implement upload_with_media in twitcurl c++ library. here’s the code that I implemented …

bool twitCurl::uploadPictureRaw( char *data, int size, std::string& newStatus )
{
	if( newStatus.empty() || !data )
    {
        return false;
    }

    /* Prepare new status message */
    std::string newStatusMsg = urlencode( newStatus );

	/* part PerformPOST */
	std::string postUrl = twitCurlDefaults::TWITCURL_PROTOCOLS[1] + twitterDefaults::TWITCURL_MEDIA_URL;	// http://api.twitter.com/1.1/statueses/upload_with_media
	std::string dataStr = twitCurlDefaults::TWITCURL_STATUSSTRING + newStatusMsg;

    /* Return if cURL is not initialized */
    if( !isCurlInit() )
    {
        return false;
    }

    std::string oAuthHttpHeader;
    struct curl_slist* pOAuthHeaderList = NULL;

    /* Prepare standard params */
    prepareStandardParams();


    /* Set http request, url and data */
    curl_easy_setopt( m_curlHandle, CURLOPT_URL, postUrl.c_str() );
    curl_easy_setopt( m_curlHandle, CURLOPT_POST, 1 );

    /* Set OAuth header */
    m_oAuth.getOAuthHeader( eOAuthHttpPost, postUrl, std::string(""), oAuthHttpHeader );
    if( oAuthHttpHeader.length() )
    {
        pOAuthHeaderList = curl_slist_append( pOAuthHeaderList, oAuthHttpHeader.c_str() );
        if( pOAuthHeaderList )
        {
            curl_easy_setopt( m_curlHandle, CURLOPT_HTTPHEADER, pOAuthHeaderList );
        }
    }
	pOAuthHeaderList = curl_slist_append(pOAuthHeaderList, "Expect:");	// for big data transmission

	curl_httppost *formpost=NULL;
	curl_httppost *lastptr=NULL;
	
	curl_formadd( &formpost, &lastptr, CURLFORM_COPYNAME, "status", CURLFORM_COPYCONTENTS, newStatusMsg.c_str(), CURLFORM_END );
	curl_formadd( &formpost, &lastptr, CURLFORM_COPYNAME, "media[]", CURLFORM_COPYCONTENTS, data, CURLFORM_CONTENTSLENGTH, size, CURLFORM_END );
	curl_easy_setopt( m_curlHandle, CURLOPT_HTTPPOST, formpost );

    /* Send http request */
    if( CURLE_OK == curl_easy_perform( m_curlHandle ) )
    {
        if( pOAuthHeaderList )
        {
            curl_slist_free_all( pOAuthHeaderList );
        }
        return true;
    }
    if( pOAuthHeaderList )
    {
        curl_slist_free_all( pOAuthHeaderList );
    }
    return false;
}

and here’s the packet

POST /1.1/statuses/update_with_media.json HTTP/1.1

Host: api.twitter.com

Accept: */*

Accept-Encoding: identity

Authorization: OAuth oauth_consumer_key="...",oauth_nonce="...",oauth_signature="...",oauth_signature_method="HMAC-SHA1",oauth_timestamp="...",oauth_token="...",oauth_version="1.0"

Content-Length: 4425

Content-Type: multipart/form-data; boundary=----------------------------54c902422ac5



------------------------------54c902422ac5

Content-Disposition: form-data; name="status"



test

------------------------------54c902422ac5

Content-Disposition: form-data; name="media[]"; filename="sample.jpg"

Content-Type: image/jpeg

(image file RAW data)

------------------------------54c902422ac5--

HTTP/1.1 403 Forbidden

content-length: 0

date: Mon, 27 Jan 2014 11:11:56 UTC

server: tfe

set-cookie: guest_id=v1%3A139082111646910282; Domain=.twitter.com; Path=/; Expires=Wed, 27-Jan-2016 11:11:56 UTC

(never mind of the url, I changed it little for test purposing)
twitter server always returns 403 with null response. I dont know what’s the problem with my transmitted packet. Is there anyone knows what’s the problem? (also you can use the edited twitcurl library at https://github.com/kuna/libtwitcurl)
thanks for reading…


#2

api.twitter.com is now HTTPS only, you cannot use HTTP anymore.


#3

now it works. thx very much.
also I found upload_with_media only works with URL upload_with_media.json, not upload_with_media/ …