我正在实施以下贝宝 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 字符串?
EDIT
AFNetworking 2.X.X 的代码 https://www.dropbox.com/sh/1sfurycnq74irf0/tiunVnOFD_
错误状态: 404 错误请求
Response :
{"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 得到了正确的响应,如下面的屏幕截图所示。