问题描述
我正在实现以下Paypal REST API:
I am implementing following paypal REST API:
curl -v https://api.sandbox.paypal.com/v1/vault/credit-card \
-H 'Content-Type:application/json' \
-H 'Authorization: Bearer {accessToken}' \
-d '{
"payer_id":"user12345",
"type":"visa",
"number":"4417119669820331",
"expire_month":"11",
"expire_year":"2018",
"first_name":"Joe",
"last_name":"Shopper"
}'
我已使用以下代码在AFNetworking 1.3.3中成功实现了此api.其中PPWebService
是AFHTTPClient
I have successfully implement this api in AFNetworking 1.3.3 with following Code. Where PPWebService
is subclass of AFHTTPClient
[[PPWebService sharedClient] setParameterEncoding:AFJSONParameterEncoding];
[[PPWebService sharedClient] setDefaultHeader:@"Content-Type" value:@"application/json"];
[[PPWebService sharedClient] setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Bearer %@", accessToken]];
[[PPWebService sharedClient] postPath:@"vault/credit-card"
parameters:creditCard
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary *response = [self JSONToObject:operation.responseString];
creditCardId = response[@"id"];
if(creditCardId)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Credit card" message:@"Saved !!" delegate:nil cancelButtonTitle:@"on" otherButtonTitles:nil];
[alert show];
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Credit card" message:error.description delegate:nil cancelButtonTitle:@"on" otherButtonTitles:nil];
[alert show];
}];
我想在我的项目中使用AFNetworking2.x.x.但是我不能用这个新版本来做.
I want to use AFNetworking 2.x.x in my project. But I am not able to do it with this new version.
我有子类AFHTTPRequestOperationManager
.我在互联网上搜索,有人建议我使用AFJSONRequestSerializer
.所有其他代码非常相似.但比起我我得到错误的请求错误.
I have subclass AFHTTPRequestOperationManager
. I search internet and people suggest me to use AFJSONRequestSerializer
. All other code is very similar. But than also I am getting bad request error.
那么如何在AFNetworking 2.x.x中使用POST方法发送原始JSON字符串?
So how can I send raw JSON string in with POST method in AFNetworking 2.x.x?
编辑
错误状态:404错误请求
响应:
{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/docs/api/#MALFORMED_REQUEST","debug_id":"ab3c1dd874a07"}
使用Postman可以得到正确的响应,如以下屏幕截图所示.
I am getting proper response by using Postman as shown in following screenshot.
推荐答案
所以最后我得到了答案.我使用AFHTTPSessionManager
的子类在我的项目中实现API.并将其与单例对象一起使用.这就是我的单例方法.
So finally I got answer. I use subclass of AFHTTPSessionManager
to implement API in my project. And use it with singleton object. So this is my singleton method.
+ (MASWebService *)APIClient
{
static MASWebService *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
NSURL *baseURL = [NSURL URLWithString:BaseURL];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_sharedClient = [[MASWebService alloc] initWithBaseURL:baseURL sessionConfiguration:config];
_sharedClient.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
_sharedClient.requestSerializer = [AFJSONRequestSerializer serializer];
[_sharedClient.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
});
return _sharedClient;
}
主线将请求序列化程序" HTTP标头"Content-Type"设置为"application/json"
The main key line is setting Request Serializer HTTP Header "Content-Type" to "application/json"
如果您没有将AFHTTPSessionManager
子类化并且对单个请求使用AFHTTPRequestOperationManager
,那么它也将起作用,因为AFHTTPRequestOperationManager
也是AFHTTPRequestSerializer
的属性,也符合AFURLRequestSerialization
协议.我还没有做,但是应该看起来像这样.
If you are not subclassing AFHTTPSessionManager
and using AFHTTPRequestOperationManager
for single request than also it will work because AFHTTPRequestOperationManager
is also conform to AFURLRequestSerialization
protocol as property of AFHTTPRequestSerializer
. I have not done it but it should look like this.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
我希望这会起作用.如果我在某个地方错了,请在评论中通知我.
I hope this will work. Inform me in comment if I am wrong somewhere.
来源: AFNetworking 2.0 POST请求工作示例代码段
这篇关于AFNetworking 2.x.x中的AFJSONParameterEncoding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!