我正在实现以下 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 获得了正确的响应,如下面的屏幕截图所示。
最佳答案
所以我终于得到了答案。我使用 AFHTTPSessionManager
的子类在我的项目中实现 API。并将其与单例对象一起使用。所以这是我的单例方法。
+ (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
协议(protocol)作为 AFHTTPRequestSerializer
的属性。我还没有这样做,但它应该看起来像这样。
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);
}];
我希望这会奏效。如果我在某个地方错了,请在评论中通知我。
来源:AFNetworking 2.0 POST request working example code snippet
关于ios - AFNetworking 2.x.x 中的 AFJSONParameterEncoding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22071188/