Add comments via HTTP POST

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
schimanke
Regular
Posts: 161
Joined: Mon Jan 07, 2008 4:38 pm
Location: Hameln, Germany
Contact:

Add comments via HTTP POST

Post 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?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Add comments via HTTP POST

Post by garvinhicking »

Hi!

Sure, comments are already added via HTTP POST method.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
schimanke
Regular
Posts: 161
Joined: Mon Jan 07, 2008 4:38 pm
Location: Hameln, Germany
Contact:

Re: Add comments via HTTP POST

Post 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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Add comments via HTTP POST

Post 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.
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
schimanke
Regular
Posts: 161
Joined: Mon Jan 07, 2008 4:38 pm
Location: Hameln, Germany
Contact:

Re: Add comments via HTTP POST

Post 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?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Add comments via HTTP POST

Post 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];
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
schimanke
Regular
Posts: 161
Joined: Mon Jan 07, 2008 4:38 pm
Location: Hameln, Germany
Contact:

Re: Add comments via HTTP POST

Post by schimanke »

Sometimes the obvious is hard to find... ;-)
Thanks! It's working now!
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: Add comments via HTTP POST

Post 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.
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/
schimanke
Regular
Posts: 161
Joined: Mon Jan 07, 2008 4:38 pm
Location: Hameln, Germany
Contact:

Re: Add comments via HTTP POST

Post 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!
Post Reply