Undo
Undo

Reputation: 25697

How to convert this HTML form to iOS

I'm trying to subscribe emails to my Sendy installation running on my EC2 server - the API docs for Sendy can be found here.

In my list, Sendy suggests that

<form action="http://www.erwaysoftware.com/sendy/subscribe" method="POST" accept-charset="utf-8">
    <label for="name">Name</label><br/>
    <input type="text" name="name" id="name"/>
    <br/>
    <label for="email">Email</label><br/>
    <input type="text" name="email" id="email"/>
    <br/>
    <label for="App">App</label><br/>
    <input type="text" name="App" id="App"/>
    <br/>
    <input type="hidden" name="list" value="my_sendy_list_key"/>
    <input type="submit" name="submit" id="submit"/>
</form>

be used in HTML to subscribe to the list. I can confirm that this works - see my website.

However, I have a need to subscribe these people from my app - through the API. Here's the code I've tried:

- (IBAction)submitButtonPressed:(id)sender
{
    self.data = [[NSMutableData alloc] initWithLength:0];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.erwaysoftware.com/sendy/subscribe/"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"[email protected]" forHTTPHeaderField:@"email"];
    [request setValue:@"John Bob" forHTTPHeaderField:@"name"];
    [request setValue:@"api_key" forHTTPHeaderField:@"list"];
    [request setValue:@"TRUE" forHTTPHeaderField:@"boolean"];

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
    [conn start];

}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.data setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
    [self.data appendData:d];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
                                 message:[error localizedDescription]
                                delegate:nil
                       cancelButtonTitle:NSLocalizedString(@"OK", @"")
                       otherButtonTitles:nil] show];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];

    // Do anything you want with it

    NSLog(@"%@", responseText);
}

The NSLog shows:

2013-04-12 15:23:03.917 Sendy API Test[13488:c07] <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="Shortcut Icon" type="image/ico" href="http://www.erwaysoftware.com/sendy/img/favicon.png">
        <title>Subscribed</title>
    </head>
    <style type="text/css">
        body{
            background: #ffffff;
            font-family: Helvetica, Arial;
        }
        #wrapper 
        {
            background: #f2f2f2;

            width: 300px;
            height: 70px;

            margin: -140px 0 0 -150px;
            position: absolute;
            top: 50%;
            left: 50%;
            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            border-radius: 5px;
        }
        p{
            text-align: center;
        }
        h2{
            font-weight: normal;
            text-align: center;
        }
        a{
            color: #000;
        }
        a:hover{
            text-decoration: none;
        }
    </style>
    <body>
        <div id="wrapper">
            <h2>Email address is invalid.</h2>
        </div>
    </body>
</html>

From this, I gather that Sendy thinks the email is invalid - in any case, the email is not registered.

Upvotes: 1

Views: 812

Answers (1)

dpassage
dpassage

Reputation: 5453

The problem is that you're setting HTTP request headers. The API is expecting the information to come in the HTTP body. Unfortunately, the NSMutableURLRequest class doesn't provide an easy way to create the body. This question provides sample code which builds the request body by hand.

That said, building the request body yourself is tricky, as the formatting rules can be arcane. Better is to use one of the open-source extensions to NSURLRequest which will take care of it for you. I'm using AFNetworking in my current project, although it's a bit more complicated to set up.

Upvotes: 2

Related Questions