如何借助AFNetworking发出Twilio api Post请求? [英] How to make Twilio api Post request with the help of AFNetworking?

查看:86
本文介绍了如何借助AFNetworking发出Twilio api Post请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 AFNetworking 访问 Twilio api。我尝试了多种方法,但没有成功。如果有人在Tiwilo上使用AFNetworking发布请求,请帮助我。

I want to access Twilio api using AFNetworking. I tried number of ways but not get success. Please help me, if anyone did Tiwilo post request using AFNetworking.

案例1:这是我的本机Objective-c工作代码。

Case 1: This is my native objective-c working code.

NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];

NSURL *url                      = [NSURL URLWithString:urlString];
NSMutableURLRequest *request    = [[NSMutableURLRequest alloc] init];

[request setURL:url];
[request setHTTPMethod:@"POST"];

NSString *bodyString    = [NSString stringWithFormat:@"From=%@&To=%@&Body=%@", from, to, message];

NSData *data            = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

    if (connectionError)
    {
        DLog(@"Error: %@", connectionError);

        completionBlock(connectionError, NO);
    }
    else
    {
        completionBlock(connectionError, YES);
    }
}];

案例2:使用AFNetorking:无效的代码:

Case 2: Using AFNetorking: Code that is not working:

代码:

    NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];

NSDictionary *dict = @{
                       @"From" : from,
                       @"To" : to,
                       @"Body" : message
                       };


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

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

相关错误:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x1775e660 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x175101b0 "Request failed: bad request (400)"}

案例3:使用AFNetorking:另一个也不起作用的代码:

Case 3: Using AFNetorking: Another Code that is also not working:

代码:

 NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];
NSURL *url                      = [NSURL URLWithString:urlString];
NSMutableURLRequest *request    = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];

NSString *bodyString    = [NSString stringWithFormat:@"From=%@&To=%@&Body=%@", from, to, message];
NSData *data            = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@", error);
}];

[operation start];

相关错误:

 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x177a3e70 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

谢谢。

推荐答案

来自Twilio的Ricky。首先是快速免责声明。直接从iOS应用程序向Twilio发送请求要求您将Twilio帐户凭据嵌入到该应用程序中,这很危险。我的建议是从应用发出请求的服务器端脚本发送SMS,以确保您的凭据安全。

Ricky from Twilio here. First off a quick disclaimer. Making a request to Twilio directly from an iOS app requires you to embed your Twilio account credentials in the app, which is dangerous. My recommendation would be to send the SMS from a server side script that your app makes a request to in order to keep your credentials safe.

话虽如此,您的代码是真的很近。默认情况下, Twilio的REST API返回XML 。如果要解析默认情况下返回的响应,则可以更新版本2中的代码以使用 AFXMLParserResponseSerializer

That being said, your code is really close. By default, Twilio's REST API returns XML. If you want to parse the response as it's returned by default you can update the code in version 2 to use the AFXMLParserResponseSerializer:

operation.responseSerializer = [AFXMLParserResponseSerializer serializer];

如果您要使用JSON,则更新Twilio URL,即发出POST请求并指示您想要JSON响应:

If you'd rather work with JSON then you update the Twilio URL you're making the POST request to and indicate you'd like a JSON response:

NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages.json", kTwilioSID, kTwilioSecret, kTwilioSID];

希望有帮助。

这篇关于如何借助AFNetworking发出Twilio api Post请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆