0
votes

I want to send String Data to a php server using the ASIHTTPRequest appendPostData method but its not working.

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"123456" dataUsingEncoding:NSUTF8StringEncoding]];
[request startAsynchronous];

I tried some modifications on the request itself like:

[request setRequestMethod:@"POST"];
[request buildPostBody];

but also this does not work.

When i use the

[request addPostValue:@"Ben" forKey:@"names"];

syntax it does work.

Anybody has an idea whats wrong here?!

1

1 Answers

1
votes

I usually use this:

- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector {

    //localCopy = self;

    self.delegate = requestDelegate;
    self.callback = requestSelector;
    self.errorCallback = errorSelector;

    NSURL *url = [NSURL URLWithString:string];

    postRequest = [[ASIFormDataRequest alloc] initWithURL:url];
    [postRequest setDelegate:self];
    [postRequest setRequestMethod:@"POST"];

    if (stringDictionary)
        for (NSString *key in [stringDictionary allKeys])
            [postRequest setPostValue:[stringDictionary objectForKey:key] forKey:key];

    if (dataDictionary)
        for (NSString *key in [dataDictionary allKeys])
            [postRequest setData:[dataDictionary objectForKey:key] forKey:key];

    //NSLog(@"request url = %@", [postRequest.url absoluteString]);
    [postRequest startAsynchronous];
}

Parameters:

  • (NSString *)string - url string, where to post your request;
  • (NSDictionary *)stringDictionary - dictionary, which contains all the text information (such as name, id etc.);
  • (NSDictionary *)dataDictionary - dictionary, which contains all data information (such as photos, files, etc.);
  • (id)requestDelegate - delegate to perform selectors below;
  • (SEL)requestSelector - selector, which will be executed while successfully request;
  • (SEL)errorSelector - selector, which will be executed, while error occurred.

P.S. Selectors will be used in the ASIHTTPRequest Delegate implementation