Objective-C でこの curl リクエストを操作しようとしています。
curl -u username:password "http://www.example.com/myapi/getdata"
私は以下を実装しましたが、データ取得エラーが発生Domain=kCFErrorDomainCFNetwork Code=303
していNSErrorFailingURLKey=http://www.example.com/myapi/getdata
ます:
// Make a call to the API to pull out the categories
NSURL *url = [NSURL URLWithString:@"http://www.example.com/myapi/getdata"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// Create the username:password string for the request
NSString *loginString = [NSString stringWithFormat:@"%@:%@", API_USERNAME, API_PASSWORD];
// Create the authorisation string from the username password string
NSData *postData = [loginString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
curl リクエストを操作しようとして間違っているところを誰かが見つけて、正しい方向に向けてくれることを期待していました。私が見逃している明らかなものはありますか?API から返されるデータは JSON 形式です。