我正在尝试使用 BigCommerce API 来使用我的 iPhone 应用程序添加客户。这就是我所拥有的。
-(IBAction)gettingcustomers {
NSString *post = @"first_name=AA&last_name=AA&email=jimbob3332002%40yahoo.com";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://myserver/customers.json"]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[connection release];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"myusername"
password:@"myapikey"
persistence:NSURLCredentialPersistenceForSession];
NSLog(@"credential created");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
NSLog(@"responded to authentication challenge");
}
else {
NSLog(@"previous authentication failure");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"RESPONSE%@", response);
if ([response isKindOfClass:[NSHTTPURLResponse class]])
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
//If you need the response, you can use it here
NSLog(@"%@", [httpResponse allHeaderFields]);
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"YAY" message:@"You exist" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"DIDFINISHLOADING");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didfailwitherror");
}
使用它,我得到连接成功并收到数据的响应,但没有发布任何内容。POST customers.json 的 API 文档说所有必需的字段都是 first_name last_name 和 email。