Topic: Site actions through the API

Posted under General

Hey all, never actually used the forums before so first and foremost guess I should say hi!

Now to business:
I've been working on a small frontend for the site's API recently, and have hit a couple roadbumps along the way, hopefully someone more knowledgeable could offer some support.

The first issue is with voting on posts through the API, I'm posting to post/vote.json with the id and score values, and the response I receive is: "HTTP Error 424: Unordered Collection." Which is honestly something I've never seen before so I couldn't begin to guess what the issue is or if it's on my end or the server's.

The second issue I'm having is when trying to post a comment. Posting to the URI comment/create.json with post_id, comment, login and password_hash, and the response is a generic 500 internal server error. This time I'm fairly certain it's a server-side issue unless I'm doing something wrong.

So yeah, any help/advice would be greatly appreciated. My first thought was something wrong with the way I'm posting the data, but actions such as logging in work just fine so I dunno.

Updated

dos_tacos said:
The first issue is with voting on posts through the API, I'm posting to post/vote.json with the id and score values, and the response I receive is: "HTTP Error 424: Unordered Collection." Which is honestly something I've never seen before so I couldn't begin to guess what the issue is or if it's on my end or the server's.

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
424 Failed Dependency (WebDAV; RFC 4918)
The request failed due to failure of a previous request (e.g., a PROPPATCH).

http://networksmart.wikidot.com/status-code-http-425
The 425 (Unordered Collection) status code indicates that the client attempted to set the position of an internal collection member in an unordered collection or in a collection with a server-maintained ordering.

Updated by anonymous

Munkelzahn said:
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
424 Failed Dependency (WebDAV; RFC 4918)
The request failed due to failure of a previous request (e.g., a PROPPATCH).

http://networksmart.wikidot.com/status-code-http-425
The 425 (Unordered Collection) status code indicates that the client attempted to set the position of an internal collection member in an unordered collection or in a collection with a server-maintained ordering.

I did look into it a little bit and found similar information, but I don't entirely understand what it means. To my knowledge the order you pass data values to a server shouldn't matter (but then again I've only worked with Python and Php when it comes to web stuff and I know the site's in Ruby, so for all I know it could make a difference).

Updated by anonymous

I have no idea what those errors actually mean, but it sounds like some sort of bug on our end. What is the 500 error you get from comment/create.json? And what are some examples of the data you're POSTing to e621 for both the vote and comment requests? (obviously minus the password hash)

Updated by anonymous

tony311 said:
What is the 500 error you get from comment/create.json? And what are some examples of the data you're POSTING to e621 for both the vote and comment requests?

I don't get any specific message past "500 Internal Server Error" so unfortunately there's not much information I can provide there. As for example data, here's an excerpt from the code and me trying to call it, since that might be easiest (sorry for the poor formatting, not sure how to get indentation to work properly):

Method to submit a comment

def submit(self):
    if not config.USERNAME and not config.PASSWORD:
        raise errors.APIUnauthorizedError('You must be logged in to post a comment.')
    data = {
        'post_id':str(self.id),
        'comment':str(self.body),
        'login':str(config.USERNAME),
        'password_hash':str(config.PASSWORD)
    }
    return api._post_data(data, config.BASE_URL + 'comment/create.json')

Method that actually posts data to the server.

def _post_data(data, url):
    err = None
    try:
        data = urllib.parse.urlencode(data)
        data = data.encode('utf-8')
    except TypeError: 
        raise errors.APIPostError('The data object is not URL-encodable.')
    try:
        req = urllib.request.Request(url, headers={'User-Agent':config.USER_AGENT})
        result = urllib.request.urlopen(req, data)
    except (urllib.error.HTTPError, ValueError, urllib.error.URLError) as err:
        raise errors.APIPostError(str(err))
    return result

Trying to submit a comment.

>>> c = esix.comment.Comment({'post_id':123456, 'body':'Test comment'})
>>> c.submit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dostacos/Documents/projects/python/esix/esix/comment.py", line 33, in submit
    return comment.submit()
  File "/home/dostacos/Documents/projects/python/esix/esix/comment.py", line 140, in submit
    api._post_data(data,config.BASE_URL+'comment/create.json'))
  File "/home/dostacos/Documents/projects/python/esix/esix/api.py", line 53, in _post_data
    if err: raise errors.APIPostError(str(err))
esix.errors.APIPostError: HTTP Error 500: Internal Server Error

Updated by anonymous

I can't figure out what's wrong with the vote request, my testing worked fine :(

About the comment thing, the params required to create a comment are 'comment[post_id]' and 'comment[body]'.

Also, 424 is a custom error code in use by us to represent "invalid parameters". Still doesn't help me, though.

Updated by anonymous

tony311 said:
About the comment thing, the params required to create a comment are 'comment[post_id]' and 'comment[body]'.

Ahhh whoops, just looked at the API documentation again and you're right, not sure how I missed that before. I just tried it though, and interestingly enough, I'm now getting a 404 error instead, though I changed nothing but the parameter names.

tony311 said:
Also, 424 is a custom error code in use by us to represent "invalid parameters". Still doesn't help me, though.

Yeah, I did see that when I first ran into the problem, but wasn't sure what was invalid about the requests I was sending.

Updated by anonymous

> api._post_data(data,config.BASE_URL+'comment/create.json'))

What is the value of config.BASE_URL in your program?

Updated by anonymous

Munkelzahn said:
> api._post_data(data,config.BASE_URL+'comment/create.json'))

What is the value of config.BASE_URL in your program?

Just "https://e621.net/", I only set it as a config variable for simplicity purposes.

Updated by anonymous

dos_tacos said:
Just "https://e621.net/", I only set it as a config variable for simplicity purposes.

You may have to set up some SSL stuff, try this :

NOTICE: If you are using cURL or another similar HTTP library and API calls seem to fail for no reason, it may be a problem with SSL. If this is the case, you will need to point cURL to a trusted certificate to compare the remote site's (e621.net) certificate with. For example, Mozilla's certificate bundle is a good one to use. Get it here and save it somewhere in your server directory. Then point cURL to the file with something like this:

curl_setopt($ch, CURLOPT_CAINFO, "/server_dir/apache/cacert.pem");

You can also simply disable SSL verification altogether, although this eliminates the advantage of using SSL in the first place:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Updated by anonymous

tony311 said:
You may have to set up some SSL stuff, try this :

I did consider that as well. Though I'm admittedly new to working with Python's internet libraries, I'm pretty sure they don't use curl (at least, urllib doesn't). It might be something among those lines though, I'll try and find some time to look into it, thanks!

Updated by anonymous

dos_tacos said:
I did consider that as well. Though I'm admittedly new to working with Python's internet libraries, I'm pretty sure they don't use curl (at least, urllib doesn't). It might be something among those lines though, I'll try and find some time to look into it, thanks!

I'm pretty sure CURL is not involved with either urllib or Requests, but you may want to consider switching to Requests and seeing if it makes a difference to these errors. (Requests is also 1000% simpler to use than urllib.)

Updated by anonymous

savageorange said:
I'm pretty sure CURL is not involved with either urllib or Requests, but you may want to consider switching to Requests and seeing if it makes a difference to these errors. (Requests is also 1000% simpler to use than urllib.)

First off, THANK YOU, not sure how I hadn't heard of this library before but it's worlds easier.

Secondly, well this is embarrassing to say the least... After switching over to using Requests I was still having issues, so I just started dumping the HTTP responses, and got this. Turns out when I was making the request, I was sending the wrong variable for the post id, it was trying to send the comment's id instead (coincidentally, I figured out the issue with voting the same way, I was posting the user's name to "username" rather than "login").

So yeah, most of this all boils down to "I'm an idiot" but regardless, it seems to be working now, so thanks everyone for the help!

Updated by anonymous

  • 1