代码之家  ›  专栏  ›  技术社区  ›  Jose

AFNetworking 2.0 AFHTTPRequestOperation PUT坏请求(400)“在iOS上

  •  0
  • Jose  · 技术社区  · 10 年前

    希望执行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的文章《How To Synchronize Core Data with a Web Service》中获得了这段示例代码,所有其他方法都有效,但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 回复  |  直到 10 年前
        1
  •  0
  •   Jose    10 年前

    过了一会儿,我发现*jsonString下面的scape序列通过了NSJSONSerialization。我不知道Chris Wagner文章“how To Synchronize Core Data with a Web Service”中的其他方法是如何工作的,但我不知道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}}];
    
    }];
    
        2
  •  0
  •   Timothy Walters    10 年前

    为什么不使用本机iOS SDK,它在幕后执行相同的REST调用。

    使用SDK,您将省去许多痛苦和精力。

    更新:

    对悲观选民来说,“不要重新发明轮子”是一个完全正确的答案。Parse提供了一个丰富的API/SDK,它有经过充分测试的代码,可以防止人们出现这些问题。

    我可以理解,出于学习的原因,我想自己动手,但除非有真正好的理由不在这种情况下使用SDK,否则我的答案是正确的。