Page 1 of 1

Add comments via HTTP POST

Posted: Sun Jul 08, 2012 7:25 pm
by schimanke
Is it possible to add comments to a blog post via HTTP POST methods?

i.e. using a certain URL like
http://www.myblog.com/index.php?url=arc ... -this-post

Anything like this?

Re: Add comments via HTTP POST

Posted: Mon Jul 09, 2012 12:36 pm
by garvinhicking
Hi!

Sure, comments are already added via HTTP POST method.

Regards,
Garvin

Re: Add comments via HTTP POST

Posted: Mon Jul 09, 2012 12:39 pm
by schimanke
Sounds good. Could you tell me how this is done or where I can find a documentation or example for that. The reason is that I'd like to implement posting comments in my iOS-app which is based on s9y.

Re: Add comments via HTTP POST

Posted: Mon Jul 09, 2012 12:43 pm
by garvinhicking
schimanke wrote:Sounds good. Could you tell me how this is done or where I can find a documentation or example for that. The reason is that I'd like to implement posting comments in my iOS-app which is based on s9y.
Simply look at the HTML <form> inside the URL you mentioned, there you have all required POST fields.

What you want to do is basically what all spambots are currently doing. Note that you might face anti-spam measurements; Captcha i.e. cannot be circumvented this way, also the issuing of a cookie will break your external POST request.

Re: Add comments via HTTP POST

Posted: Mon Jul 09, 2012 2:53 pm
by schimanke
Yeah, I got that. Could you give me an example how such a call would look like? I need only three fields (name, e-Mail, comment).

My current aproach in Objective-C would look like this:

Code: Select all

NSURL *url = [NSURL URLWithString:@"http://www.schimanke.com/index.php?url=archives/3485-DigiTimes-Neuer-iMac-angeblich-im-Oktober.html"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *requestBody = [@"name=Flo&comment=Das finde ich richtig gut!" dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:requestBody];
    NSURLResponse *response = NULL;
    NSError *requestError = NULL;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
    NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
But it doesn't seem to work. What it does is it calls the URL http://www.schimanke.com/index.php?url= ... tober.html and tries to POST "name=Flo&comment=Das finde ich richtig gut!"

I guess I have to specify that it is a comment. But where do I do that?

Re: Add comments via HTTP POST

Posted: Mon Jul 09, 2012 2:56 pm
by garvinhicking
Hi!

Your POST request would need to look like this:

Code: Select all

POST /index.php?url=archives/3485-DigiTimes-Neuer-iMac-angeblich-im-Oktober.html HTTP/1.1
Host: www.schimanke.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://www.schimanke.com/index.php?url=archives/3485-DigiTimes-Neuer-iMac-angeblich-im-Oktober.html
Content-Type: application/x-www-form-urlencoded
Content-Length: 291
serendipity%5Bentry_id%5D=3485&serendipity%5Bname%5D=This+is+my+name&serendipity%5Bemail%5D=mail%40example.com&serendipity%5Burl%5D=http%3A%2F%2Fwww.example.com&serendipity%5BreplyTo%5D=0&serendipity%5Bcomment%5D=This+is+my+reply%2C+test+capture.&serendipity%5Bsubmit%5D=Kommentar+abschicken
You are missing the serendipity[...] prefix in your Post request, I think.

I think this should work:

Code: Select all

NSURL *url = [NSURL URLWithString:@"http://www.schimanke.com/index.php?url=archives/3485-DigiTimes-Neuer-iMac-angeblich-im-Oktober.html"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *requestBody = [@"serendipity[entry_id]=3485&serendipity[name]=Flo&serendipity[comment]=Das+finde+ich+richtig+gut!&serendipity[submit]=Kommentar+abschicken" dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:requestBody];
    NSURLResponse *response = NULL;
    NSError *requestError = NULL;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
    NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];

Re: Add comments via HTTP POST

Posted: Mon Jul 09, 2012 3:08 pm
by schimanke
Sometimes the obvious is hard to find... ;-)
Thanks! It's working now!

Re: Add comments via HTTP POST

Posted: Tue Jul 10, 2012 2:37 pm
by blog.brockha.us
Only to mention this: For a client it should be better to use the XMLRPC Api of s9y. If you want to know how, you can have a look at the iOS Wordpress client. It's open source.

But if you want to comment only, POSTing is good enough, I think.

Re: Add comments via HTTP POST

Posted: Tue Jul 10, 2012 2:41 pm
by schimanke
Yep, it's just about giving my readers the option to post comments through my app. Everything else already works pretty fine. But thanks for your suggestion!