CPTextField / CPURLRequest question

55 views
Skip to first unread message

Calvin

unread,
Jan 6, 2010, 12:56:16 PM1/6/10
to Cappuccino & Objective-J
Hello,

I have a simple Sinatra app and I am trying to integrate Cappuccino
into it as an experiment. In the version of the Sinatra app with no
Cappuccino, there is a form with an action of "/data" and when you
submit the form, it takes you to the "localhost:4567/data" page and
displays the input from the form.

My question: If I can set the "action" of a html form, how would I set
the "action" of a CPTextField or CPURLRequest to make the app behave
the same way? Could I use - setHTTPBody: in the URL request?

here is part of the form markup:

<form action="/data" method="post" accept-charset="utf-8">
</form>

and here is some the code from my AppController.j file :

- (@action)buttonPressed:(id)sender
{
var request = [[CPURLRequest alloc] initWithURL:"http://localhost:
4567/data?input="+[input objectValue]];
[request setHTTPMethod:"POST"];
testConnection = [[CPURLConnection alloc] initWithRequest:request
delegate:self];
}

Thanks for your time,

Calvin

Elias

unread,
Jan 6, 2010, 1:39:53 PM1/6/10
to Cappuccino & Objective-J
You can enhance your request action like this:

var request = [[CPURLRequest alloc] initWithURL:"http://localhost:4567/

data";
[request setHTTPMethod:"POST"];
[request setHTTPBody:"input=" + escape([input objectValue])];
[request setValue:"application/x-www-form-urlencoded"
forHTTPHeaderField:"Content-Type"];


testConnection = [[CPURLConnection alloc] initWithRequest:request
delegate:self];


if the request returns some information you can use it with the
following delegate:

- (void)connection:(CPURLConnection)aConnection didReceiveData:
(CPString)data
{
alert(data);

Calvin

unread,
Jan 6, 2010, 2:13:33 PM1/6/10
to Cappuccino & Objective-J
Thanks- the information shows up in the Cappuccino interface, but not
on the "http://localhost:4567/data" page where I want it to show up.
Is it not possible to add a Cappuccino equivalent of a HTML form
action to a URLRequest?

luddep

unread,
Jan 7, 2010, 1:01:17 AM1/7/10
to Cappuccino & Objective-J
That's exactly what you do when you set the method to POST, the
content-type header to application/x-www-form-urlencoded and place a
series of key-value pairs in HTTPBody, which look pretty much just
like a query string, e.g, mykey=myvalue&myotherkey=anothervalue.

What Elias posted is the equivalent of a straight up html form:

<form action="http://localhost:4567/data" method="post">
<input type="text" name="input" value="my value" />
<button>Submit</button>
</form>

Reply all
Reply to author
Forward
0 new messages