如何使用AFNetworking在iOS目标c中发出此帖子请求

时间:2014-08-06 06:26:22

标签: ios post afnetworking twilio afnetworking-2

我需要在iOS中的twilio documentation发送与此相当的POST请求以发送短信

curl -XPOST https://api.twilio.com/2010-04-01/Accounts/AC5ef8732a3c49700934481addd5ce1659/Messages.json \
-d "Body=Jenny%20please%3F%21%20I%20love%20you%20<3" \
-d "To=%2B15558675309" \
-d "From=%2B14158141829" \
-u 'AC5ef8732a3c49700934481addd5ce1659:{AuthToken}'

这就是我所拥有的:

NSString *kTwilioSID = @"AC5ef8732a3c49700934481addd5ce1659";
NSString *kTwilioSecret = @"SECRET";
NSString *kFromNumber = @"+14158141829";
NSString *kToNumber = @"+15558675309";
NSString *kMessage = @"Jenny please?! I love you <3";


NSString *twilioURL = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/Messages.json", kTwilioSID, kTwilioSecret, kTwilioSID];
NSDictionary *params = @ {@"account" :kTwilioSID, @"from":kFromNumber, @"to":kToNumber, @"body":kMessage };

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:twilioURL parameters:params
      success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSLog(@"Success: %@", responseObject);
 }
      failure:
 ^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error);
 }];

错误失败,&#34;请求失败:错误请求(400)&#34;,并且短信也没有被发送 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

我建议你不要使用

[manager POST:twilioURL parameters:params ....

这种方法对我不起作用。相反,这里是一个有效的代码示例:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", @"http://myurl/actionToperform"]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//1
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"{ \"name\" : \"%@\", \"polygon\" : [%@]}",[alertView textFieldAtIndex:0].text, concatCoord] dataUsingEncoding:NSUTF8StringEncoding]];

//post
[request setHTTPBody:postBody];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //Do things if succesfull
    NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //Do things when you've got an error
    NSLog(@"error: %@", error);
}];

[operation start];

请注意,我在此代码中使用JSON来格式化我的参数(1)。

祝你好运。

答案 1 :(得分:0)

此问题可以修复,但您必须在serializerRequest对象上设置AFHTTPRequestOperationManager

如果我更正了此错误的原因是您没有设置标题字段。做这样的事情。

AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

NSString pairString = [NSString stringWithFormat: @"%@", someRandomId];

NSData pairData = [pairString dataUsingEncoding NSUTF8StringEncoding];

NSString *serializerVal = [NSString stringWithFormat:@"%@", [pairData base64EncodedStringWithOptions:0];

[requestSerializer setValue: serializerVal forHTTPHeaderField:@"Auth"];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager setRequestSerializer:requestSerializer];