0

I'm looking to create a cocoa program that simply sends data typed into text fields to an HTTP form.

I have all of the various connection details worked out, I just can't seem to get the posted string to target the text fields. This is what I have for the form:

<html>
<body>

<form action='test.php' method='post'>
    Name: <input type='text' name='nameOfPerson'/>
    Age:  <input type='text' name='ageOfPerson'/>
    <input type='submit'/>
</form>

</body>
</html>

and for the cocoa...

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                    initWithURL:[NSURL URLWithString:@"http://localhost:8888/CocoaFormPractice/form.html"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"text/html" forHTTPHeaderField:@"Content-type"];

    NSString *xmlString = @"nameOfPerson:test&ageOfPerson:123";

    [request setValue:[NSString stringWithFormat:@"%d", [xmlString length]] forHTTPHeaderField:@"Content-length"];
    [request setHTTPBody:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [request release];

I know that the value I'm using in the xmlString is bogus, and not even XML.

Adam Wenger
  • 17,100
  • 6
  • 52
  • 63
patrickn
  • 2,501
  • 4
  • 22
  • 21

1 Answers1

0

You are mostly on the right track:

Replace colons ":" in your xmlString with "=" sign.

NSString *xmlString = @"nameOfPerson=test&ageOfPerson=123";

Also, it's not XML, so you can just call it "queryString" instead.

Sanjay Chaudhry
  • 3,181
  • 1
  • 22
  • 31
  • Great, thanks Sanjay - I'm still not getting the posted values to the form, but I suspect it must be something else I've bumbled in my code, and not your suggestion. – patrickn Nov 21 '11 at 04:40
  • What you're using to send the request to the server? Check out the following:[link](http://stackoverflow.com/questions/5537297/ios-how-to-perform-a-http-post-request/5537501#5537501) – Sanjay Chaudhry Nov 21 '11 at 04:52