gpt4 book ai didi

ios - 方括号之间的 Restkit request.body 值

转载 作者:行者123 更新时间:2023-11-29 12:45:42 24 4
gpt4 key购买 nike

我正在执行一个非常基本的 POST 请求。将我的 requestSerializationMIMEType 设置为 RKMIMETypeFormURLEncoded 作为我的服务器所期望的(尽管这是默认设置)。

现在在日志中,request.body 看起来像这样:

request.body=[param1]=test&[param2]=2724

在服务器中产生未知的表单值。

这里的问题是参数名称,它们在方括号之间,我没有解释为什么它们被这样序列化!

我的代码在某种程度上与 github 上提供的示例完全相同.

注意:我在没有方括号的情况下手动完成了 POST 请求,并且工作正常。

编辑

代码

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[GenericResponse class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"error":@"error", @"status":@"status"}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *genericDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"" keyPath:@"" statusCodes:statusCodes];

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:[User generateJSONMapping]];//dictionary mapping
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"" method:RKRequestMethodPOST];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://foo.com/rest/createUser/"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:genericDescriptor];
manager.requestSerializationMIMEType=RKMIMETypeFormURLEncoded;

// POST to create
[manager postObject:user path:@"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"success");// the request is success but the params are not delivered to the server
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"fail");
}];

我猜 User 非常简单

@interface User : NSObject
@property NSNumber *idShort;
@property NSNumber *idLong;
@property NSNumber *idCom;
@property NSString *comment;
@property NSString *token;

+ (NSDictionary *) generateJSONMapping;
@end


@implementation User
+ (NSDictionary *) generateJSONMapping
{
return @{
@"idShort": @"idShort",
@"idLong": @"idLong",
@"idCom":@"idCom",
@"comment":@"comment",
@"token":@"token",
};
}
@end

最佳答案

问题出在 RKRequestDescriptor 中,将 rootKeyPath 作为空字符串传递会导致此问题。传递 nil 将解决问题。

RKRequestDescriptor *requestDescriptor = [
RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[User class]
rootKeyPath:nil
method:RKRequestMethodPOST];

关于ios - 方括号之间的 Restkit request.body 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23741926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com