AFNetworking 2.0 AFHTTPRequestOperation PUT错误请求(400)"在iOS上

时间:2014-08-06 04:18:51

标签: ios xcode parse-platform afnetworking-2

希望执行Parse Cloud REST功能PUT增加值

 curl -X PUT \
   -H "X-Parse-Application-Id: RFtvvfffdfgwbeERRGFGFFGFHNIc6ubgwpJ5LL" \
   -H "X-Parse-REST-API-Key: SsYTGDFGDSFSDHGGGFTY56TXC435GGFhrfs0O4u0K" \
   -H "Content-Type: application/json" \
   -d '{"qtyAllocated":{"__op":"Increment","amount":1}}' \
   https://api.parse.com/1/classes/Lot/WrXPiXtB89

我从Chris Wagner的文章中找到了如何使用Web服务同步核心数据的文章,所有其他方法都有效但不是PUT,这个返回时出现此错误

AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8dbc210> { URL: https://api.parse.com/1/classes/Lot/WrXPiXtB89 } { status code: 400, headers {
    "Access-Control-Allow-Origin" = "*";
    "Access-Control-Request-Method" = "*";
    "Cache-Control" = "no-cache";
    Connection = "keep-alive";
    "Content-Length" = 89;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 06 Aug 2014 03:24:57 GMT";
    Server = "nginx/1.6.0";
    "Set-Cookie" = "_parse_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiIlZjAzNDQxZDgwZDI5MDBhMjJhYzVjYTFjMGY2MzhiNTk%3D--bd6b3ec8f339e5928d44f29875b8f0d22e8f88fc; domain=.parse.com; path=/; expires=Fri, 05-Sep-2014 03:24:57 GMT; secure; HttpOnly";
    Status = "400 Bad Request";
    "X-Content-Type-Options" = nosniff;
    "X-Runtime" = "0.047238";
    "X-UA-Compatible" = "IE=Edge,chrome=1";
} }, NSLocalizedDescription=Request failed: bad request (400)}

在AFHTTPRequestOperationManager的子类中

+ (instancetype)sharedSDAFParseAPIClient {
    static SDAFParseAPIClient *sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedClient = [[SDAFParseAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kSDFParseAPIBaseURLString]];
    });

    return sharedClient;
}

-(instancetype)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self) {
        AFJSONRequestSerializer *requestSerializer = [[AFJSONRequestSerializer alloc] init];
        [requestSerializer setValue:kSDFParseAPIApplicationId forHTTPHeaderField:@"X-Parse-Application-Id"];
        [requestSerializer setValue:kSDFParseAPIKey forHTTPHeaderField:@"X-Parse-REST-API-Key"];
        [self setRequestSerializer:requestSerializer];
        [self setResponseSerializer:[AFJSONResponseSerializer serializer]];
    }

    return self;
}

- (AFHTTPRequestOperation *)UPDATERequestForClass:(NSString *)className
                                  forObjectWithId:(NSString *)objectId
                                           parameters:(NSDictionary *)field
                                          success:(SuccessBlockType)success
                                          failure:(FailureBlockType)failure {

   __block NSDictionary *parameters = nil;

    [field enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL* stop) {

        NSString *jsonString = [NSString stringWithFormat:@"{\"__op\":\"Increment\",\"amount\":%@}",value];

       parameters = [NSDictionary dictionaryWithObject:jsonString forKey:key];

    }];

    AFHTTPRequestOperation *operation = [self PUT:[NSString stringWithFormat:@"classes/%@/%@", className, objectId] parameters:parameters success:success failure:failure];
    return operation;
}

任何帮助都将受到高度赞赏

2 个答案:

答案 0 :(得分:0)

过了一会儿,我发现* jsonString上的scape序列低于传递NSJSONSerialization的地方。我不知道Chris Wagner的其他方法如何运作&#34;如何使用Web服务同步核心数据&#34;但是不能在PUT上工作,至少对我而言。

        NSString *jsonString = [NSString stringWithFormat:@ {\"__op\":\"Increment\",\"amount\":%@}",value];

由于我使用参数数组作为源,最后我将* jsonString转换为:

__block NSMutableDictionary *parameters = [NSMutableDictionary dictionary];

[field enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL* stop) {

   [parameters addEntriesFromDictionary:@{key: @{@"__op":@"Increment", @"amount":value}}];

}];

答案 1 :(得分:0)

为什么不使用本机iOS SDK,它会进行相同的REST调用。

使用SDK可以为您节省很多痛苦和精力。

更新:

对于downvoters来说,说“不要重新发明轮子”是完全有效的答案。 Parse提供了一个丰富的API / SDK,它具有经过全面测试的代码,可以防止人们遇到这些问题。

我可以理解为了学习原因而想要自己做,但除非在这种情况下有一些非常好的理由不使用SDK,我的答案就是。