简体   繁体   中英

Posting data to a Wufoo form in Objective C

I'm trying to integrate the Wufoo API into an iPhone Application, and having difficulty at the last stage. I have used AFNetworking to make the connection to the Wufoo form successfully, having made a subclass of AFHTTPClient to accommodate the extra headers required, such as HTTP authorisation. I've also set Parameter Encoding to AFJSONParameterEncoding.

When I make a request using the above, it successfully connects to the server and posts the data, but an error comes back to do with blank fields. I've added the Field/Key pairs in an NSDictionary to be passed in the request, but they must be in the wrong format or something because they're not getting processed at the other end. Sorry, I know it's a lengthy question, but any help would be appreciated :). I've appended the response I get in the console, and the relevant class files/methods.

Response:

2013-02-14 12:11:43.053 <AppName>[14750:c07] Success?: 0
Error: Errors have been <b>highlighted</b> below.
Fields: (
        {
        ErrorText = "This field is required. Please enter a value.";
        ID = Field1;
    },
        {
        ErrorText = "This field is required. Please enter a value.";
        ID = Field3;
    },
        {
        ErrorText = "This field is required. Please enter a value.";
        ID = Field222;
    },
        {
        ErrorText = "This field is required. Please enter a value.";
        ID = Field11;
    },
        {
        ErrorText = "This field is required. Please enter a value.";
        ID = Field12;
    },
        {
        ErrorText = "This field is required. Please enter a value.";
        ID = Field220;
    }
)

Class Files:

ViewController.m

 -(NSDictionary *)getParameters {

        NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
                       emailAddress, @"Field1", //problem
                       firstName, @"Field3", //problem
                       lastName, @"Field4", 
                       activityArranged, @"Field10",
                       evidenceDescription, @"Field222", // problem
                       startDate, @"Field11", //Problem
                       endDate, @"Field224",
                       @"1", @"Field12", //Problem
                       benefitExplanation, @"Field113",
                       activityCategory, @"Field116",
                       webAddress, @"Field219",
                       @"I Agree", @"Field220", //Problem
                       nil];

        return d;
   }

-(void)submitForm {
            DiaryForm *df = [[DiaryForm alloc] init];
            [df submitForm:self params:[self getParameters]];
}

DiaryForm.m

-(void)submitForm:(id)sender params:(NSDictionary *)params {
WufooAPIClient *client = [WufooAPIClient sharedClient];
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
    NSURLRequest *req = [client requestWithMethod:@"POST" path:@"entries.json" parameters:params];
    AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:req success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
        NSLog(@"Success?: %@\nError: %@\nFields: %@",[JSON objectForKey:@"Success"], [JSON objectForKey:@"ErrorText"], [JSON objectForKey:@"FieldErrors"]);


    }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];

        NSLog(@"[Error]: (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);

    }];
    [op start];

}

WufooAPIClient.m

#import "WufooAPIClient.h"
#import "AFNetworking.h"


@implementation WufooAPIClient

+(WufooAPIClient *)sharedClient {
    static WufooAPIClient *_sharedClient = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        NSString *kProtocol = @"https";
        NSString *kSub = @"<removed>";
        NSString *kHost = @"wufoo.com";
        NSString *kHash = @"<removed>";
        NSString *sURL = [NSString stringWithFormat:@"%@://%@.%@/api/v3/forms/%@/", kProtocol, kSub, kHost, kHash];
        NSLog(sURL);
        NSURL *url = [NSURL URLWithString:sURL];
        _sharedClient = [[self alloc] initWithBaseURL:url];
    });
    return _sharedClient;
}

-(id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setDefaultHeader:@"Accept" value:@"application/json"];
    [self setAuthorizationHeaderWithUsername:@"<removed>" password:@"<removed>"];
    self.parameterEncoding = AFJSONParameterEncoding;

    return self;
}

@end

Fixed the issue by changing a line in the WufooAPIClient.m class, initWithBaseURL: method.

This:

self.parameterEncoding = AFJSONParameterEncoding;

To:

 self.parameterEncoding = AFFormURLParameterEncoding;

Wufoo works by sending HTTP POST data, then send you back either a json or xml response. What I had done was to set the data sent to be JSON data rather than Form Data as it should have been.

Hope that helps anyone trying to do the same thing :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM