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

Flickr returns error on POST request without Content-Length in the headers (OS dependent) #11

Closed
morozgrafix opened this issue Jun 2, 2014 · 1 comment · Fixed by #12

Comments

@morozgrafix
Copy link
Contributor

While running python-flickr from some *nix systems and making a POST request to Flickr API - Flickr will return non JSON page like this:

screenshot 2014-06-01 22 21 33

Same code works on OS X 10.9.X, but producing an error on RHEL or Ubuntu system. The reason why this is happening is that when Content-Length is not specified in the headers of the request, Flickr Apache Traffic Server will return HTTP/1.1 400 Content Length Required error and then redirect to page shown above.

Here are some traces that I pulled by turning on httplib2 debugger on. (with my API info redacted):

From OS X - works as expected:

connect: (api.flickr.com, 443)
send: 'POST /services/rest?api_key=<API_KEY>&nojsoncallback=1&method=flickr.photos.comments.addComment&format=json&comment_text=This+is+a+test+comment.&photo_id=14114731790 HTTP/1.1\r\nHost: api.flickr.com\r\nContent-Length: 0\r\ncontent-type: application/json\r\naccept-encoding: gzip, deflate\r\nauthorization: OAuth realm="https://api.flickr.com", oauth_body_hash="<OAUTH_BODY_HASH>", oauth_nonce="89465404", oauth_timestamp="1401523858", oauth_consumer_key="<API_KEY>", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_token="<OAUTH_TOKEN>", oauth_signature="<OAUTH_SIGNATURE>"\r\nuser-agent: Python-Flickr v0.3.0\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Sat, 31 May 2014 08:10:58 GMT
header: Content-Type: application/json
header: Content-Length: 249
header: P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
header: Cache-Control: private
header: X-Served-By: www273.flickr.bf1.yahoo.com
header: Vary: Accept-Encoding
header: Content-Encoding: gzip
header: Age: 0
header: Via: http/1.1 fts106.flickr.bf1.yahoo.com (ApacheTrafficServer/4.0.2 [cMsSf ]), http/1.1 r04.ycpi.lax.yahoo.net (ApacheTrafficServer/4.0.2 [cMsSf ])
header: Server: ATS
header: Connection: keep-alive
{'comment': {'permalink': 'https://www.flickr.com/photos/deadlypinkrhino/14114731790/#comment72157644519164118', 'realname': 'Deadly Pink Rhino', 'path_alias': 'deadlypinkrhino', 'author': '60941703@N06', 'datecreate': '1401496262', '_content': 'This is a test comment.', 'authorname': 'DeadlyPinkRhino', 'id': '60896381-14114731790-72157644519164118'}, 'stat': 'ok'}

From Ubuntu:

connect: (api.flickr.com, 443)
send: 'POST /services/rest?api_key=<API_KEY>&nojsoncallback=1&method=flickr.photos.comments.addComment&format=json&comment_text=This+is+a+test+comment.&photo_id=14114731790 HTTP/1.1\r\nHost: api.flickr.com\r\ncontent-type: application/json\r\naccept-encoding: gzip, deflate\r\nauthorization: OAuth realm="https://api.flickr.com", oauth_body_hash="<OAUTH_BODY_HASH>", oauth_nonce="72642461", oauth_timestamp="1401523684", oauth_consumer_key="<API_KEY>", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_token="<OAUTH_TOKEN>", oauth_signature="<OAUTH_SIGNATURE>"\r\nuser-agent: Python-Flickr v0.3.0\r\n\r\n'
reply: 'HTTP/1.1 400 Content Length Required\r\n'
header: Date: Sat, 31 May 2014 08:08:04 GMT
header: Connection: close
header: Via: http/1.1 r15.ycpi.gq1.yahoo.net (ApacheTrafficServer/4.0.2 [c s f ])
header: Server: ATS
header: Cache-Control: no-store
header: Content-Type: text/html; charset=utf-8
header: Content-Language: en
header: Content-Length: 1464
Traceback (most recent call last):
  File "addComment_test.py", line 12, in <module>
    add_comment = flickr_api.post('flickr.photos.comments.addComment',params={'photo_id': '14114731790', 'comment_text': 'This is a test comment.'})
  File "/usr/local/lib/python2.7/dist-packages/flickr.py", line 317, in post
    return self.api_request(endpoint, method='POST', params=params, files=files, replace=replace)
  File "/usr/local/lib/python2.7/dist-packages/flickr.py", line 298, in api_request
    raise FlickrAPIError('Content is not valid JSON, unable to be decoded.')
flickr.FlickrAPIError: 'Content is not valid JSON, unable to be decoded.'

I already have a patch to correct this problem that I will be submitting and this is a result from the same system that used to surface this issue.

connect: (api.flickr.com, 443)
send: 'POST /services/rest?api_key=<API_KEY>&nojsoncallback=1&method=flickr.photos.comments.addComment&format=json&comment_text=This+is+a+test+comment.&photo_id=14114731790 HTTP/1.1\r\nHost: api.flickr.com\r\ncontent-length: 0\r\ncontent-type: application/json\r\naccept-encoding: gzip, deflate\r\nauthorization: OAuth realm="https://api.flickr.com", oauth_body_hash="<OAUTH_BODY_HASH>", oauth_nonce="55711331", oauth_timestamp="1401524098", oauth_consumer_key="<API_KEY>", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_token="<OAUTH_TOKEN>", oauth_signature="<OAUTH_SIGNATURE>"\r\nuser-agent: Python-Flickr v0.3.0\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Sat, 31 May 2014 08:14:58 GMT
header: Content-Type: application/json
header: Content-Length: 249
header: P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
header: Cache-Control: private
header: X-Served-By: www223.flickr.bf1.yahoo.com
header: Vary: Accept-Encoding
header: Content-Encoding: gzip
header: Age: 0
header: Via: http/1.1 fts119.flickr.bf1.yahoo.com (ApacheTrafficServer/4.0.2 [cMsSf ]), http/1.1 r11.ycpi.sjb.yahoo.net (ApacheTrafficServer/4.0.2 [cMsSf ])
header: Server: ATS
header: Connection: keep-alive
{u'comment': {u'permalink': u'https://www.flickr.com/photos/deadlypinkrhino/14114731790/#comment72157644519164118', u'realname': u'Deadly Pink Rhino', u'path_alias': u'deadlypinkrhino', u'author': u'60941703@N06', u'datecreate': u'1401496262', u'_content': u'This is a test comment.', u'authorname': u'DeadlyPinkRhino', u'id': u'60896381-14114731790-72157644519164118'}, u'stat': u'ok'}
morozgrafix added a commit to morozgrafix/python-flickr that referenced this issue Jun 2, 2014
Update for upload/replace end points - fixes michaelhelmick#10
Added explicit setting of “Content-Length” for POST requests to fix michaelhelmick#11
@morozgrafix
Copy link
Contributor Author

Credit goes to @pumpikano for actually identifying the issue with "Content-Length" headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant