我正在实现以下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
[[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];
}];
我想在我的项目中使用AFNetworking 2.x.x.但我无法用这个新版本来做。
我有子类AFHTTPRequestOperationManager
。我搜索互联网,人们建议我使用AFJSONRequestSerializer
。所有其他代码非常相似。但是,我也得到了错误的请求错误。
那么如何在AFNetworking 2.x.x中使用POST方法发送原始JSON字符串?
修改
错误状态: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获得了正确的响应,如下面的屏幕截图所示。
答案 0 :(得分:1)
所以最后我得到了回答。我使用AFHTTPSessionManager
的子类在我的项目中实现API。并将其与singleton对象一起使用。所以这是我的单身方法。
+ (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;
}
主要关键是设置Request Serializer HTTP Header" Content-Type"到" application / json"
如果您没有为AFHTTPSessionManager
创建子类并将AFHTTPRequestOperationManager
用于单个请求,那么它也可以正常工作,因为AFHTTPRequestOperationManager
也符合AFURLRequestSerialization
协议作为{{1}的属性}}。我没有这样做,但看起来应该是这样的。
AFHTTPRequestSerializer
我希望这会奏效。如果我在某处错了,请通知我。
来源:AFNetworking 2.0 POST request working example code snippet
答案 1 :(得分:0)
那么如何在AFNetworking 2.x.x中使用POST方法发送原始JSON字符串?
尝试在AFHTTPRequestOperationManager中使用SOAP + XML数据时遇到了同样的问题。显然,请求序列化程序应该是子类,以满足您的特殊序列化需求,但这种方式似乎更容易:
NSError *error = nil;
NSData *envelopeData = [NSJSONSerialization dataWithJSONObject:params options:options error:nil];
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST"
URLString:path
parameters:nil
error:&error];
// In my case, I also needed the following:
// [request setValue:action forHTTPHeaderField:@"SOAPAction"];
[request setHTTPBody:envelopeData];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] init];
operation = [self HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
// parse response here
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// parse response here
}];
它劫持了AFHTTPRequestOperationManager创建的请求序列化程序,将参数数据插入HTTP正文,然后将此新请求传递给AFHTTPRequestOperation。