TON API 400 (twurl/ton-upload vs curl)

ton-upload
ton

#1

Hi guys! I’m kind of at my wits end, trying to find the difference between the curl and the twul calls. Please see below.

twurl/ton-upload:

<- "POST https://ton.twitter.com/1.1/ton/bucket/ta_partner HTTP/1.1
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: /
User-Agent: OAuth gem v0.4.7
Content-Type: text/comma-separated-values
Content-Length: 12261
X-Ton-Expires: Sun, 20 Mar 2016 16:12:39 GMT
Authorization: OAuth oauth_body_hash=“REDACTED”, oauth_consumer_key=“REDACTED”, oauth_nonce=“REDACTED”, oauth_signature=“REDACTED”, oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“REDACTED”, oauth_token=“REDACTED”, oauth_version=“1.0"
Connection: close\r\nHost: ton.twitter.com\r\n\r\n”

curl:

POST /1.1/ton/bucket/ta_partner HTTP/1.1
Host: ton.twitter.com
Accept: /
Content-Length: 12261
X-TON-Expires: Tue, 15 Mar 2016 16:12:30 GMT
Authorization: OAuth oauth_consumer_key=“REDACTED”, oauth_nonce=“REDACTED”, oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“REDACTED”, oauth_version=“1.0”, oauth_token=“REDACTED”, oauth_signature="REDACTED%3D"
Content-Type: text/comma-separated-values; boundary=------------------------25aceae34d75d12f

First difference I can see is oauth_body_hash, but I can’t seem to find any twitter documentationas to what I’m supposed to put there. There’s also the “boundary” added by curl.

Any thoughts as to what I’m missing here?


#2

Hi @silverpop_ea! So the twurl call succeeds and the cURL call does not? If yes, what error do you receive for the cURL call?


#3

HTTP/1.1 400 Bad Request
< cache-control: no-cache
< content-length: 0
< date: Thu, 10 Mar 2016 16:12:23 GMT
< server: tsa_a
< set-cookie: guest_id=v1%3A145762634383261612; Domain=.twitter.com; Path=/; Expires=Sat, 10-Mar-2018 16:12:23 UTC
< strict-transport-security: max-age=631138519
< x-connection-hash: d6ebefd610faece8fe17da88738cac1f
< x-content-type-options: nosniff
< x-rate-limit-limit: 50
< x-rate-limit-remaining: 48
< x-rate-limit-reset: 1457627079
< x-response-time: 13
< x-tsa-request-body-time: 74

  • HTTP error before end of send, stop sending

(not sure if I need to redact parts of that)

It seems like there is just 400, and no details past that.


#4

Looks like I’ve found the solution.

The problem is boundary=------------------------25aceae34d75d12f after all.

Twitter does not accept “application/x-www-form-urlencoded” (per https://dev.twitter.com/rest/ton).

I was using

$postdata = array(
            'file_contents' => '@'.$payload
        );

along with setting header:

'Content-Type: ' . $content_type
Unfortunately, this appears to just patch over the fact that php considers it to be x-www-form-urlencoded.

So, my solution was to change payload to:

$postdata = file_get_contents($payload);
Once this was done, requests shifted to 201’s.

God I hope this helps someone else - this was surprisingly annoying to solve.


#5

Way to go! :slight_smile: I’m glad you solved this!

Please don’t forget to mark your last answer, with the solution, as being the solution.