1

RestKit 0.2 を使用して簡単な投稿を行う際に問題が発生しています。これは私のコードがどのように見えるかです:

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[@"title"]];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[Feed class] rootKeyPath:@""]

NSURL *baseURL = [NSURL URLWithString:@"http://some.url.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];

RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

[objectManager addRequestDescriptor:requestDescriptor];

Feed *feed = [[Feed alloc] init];
feed.title = @"SomeTitle";

[objectManager postObject:feed path:/feeds parameters:nil success:nil failure:nil];

上記のコードを実行すると、次のログが表示されます。

request.headers={
    Accept = "application/json";
    "Accept-Language" = "sv, en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8";
    "Content-Type" = "application/x-www-form-urlencoded; charset=utf-8";
    "User-Agent" = "appName/1.2 (iPhone; iOS 5.1.1; Scale/2.00)";
}
request.body=[title]=Foobar
2013-02-13 12:05:55.450 appName[3672:330f] T restkit.network:RKHTTPRequestOperation.m:177 POST 'some.url.com' (400 Bad Request) [0.5037 s]:
response.headers={
    Connection = "keep-alive";
    "Content-Length" = 99;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 13 Feb 2013 11:05:55 GMT";
    "Set-Cookie" = "cookieTextHere; domain=.some.domain.com; path=/; expires=Wed, 27-Feb-2013 11:05:55 GMT; HttpOnly";
    "X-Request-Id" = xxe131e73e638733519c44xxa6224aa3f2xx;
}
response.body={"title":"JSON Parser Error","errors":
"lexical error: invalid string in json text. [title]=Foobar"}

REST サーバーは投稿が JSON 形式であることを想定していますが、上記のログを見ると、リクエストの content-type が「application/x-www-form-urlencoded」に設定されていることがわかります。これが問題の原因ですか? 私はこれを使用して変更しようとしました

[client setDefaultHeader:@"Content-Type" value:RKMIMETypeJSON];

...しかし、それはうまくいきませんでした。

www.hurl.it を使用して {"title":"Foobar"} のような生の json データを問題なく URL に投稿しました。

何か案は?

4

1 に答える 1

0

Blake Watters は、restkit の Google グループに返信しています

以下、それは私の実装です:

  1. 管理オブジェクトの場合は、RKManagedObjectRequestOperation のサブクラス。

EPObjectRequestOperation.h

#import <RestKit/RestKit.h>

@interface EPObjectRequestOperation : RKManagedObjectRequestOperation

@end

EPObjectRequestOperation.m

#import "EPObjectRequestOperation.h"

@interface EPObjectRequestOperation ()

@property (nonatomic, strong, readwrite) NSError *error;
@end

@implementation EPObjectRequestOperation

- (void) main
{
    [super main];
    if([self.HTTPRequestOperation hasAcceptableStatusCode] && self.error.code == -1016) {//Expected content type

        self.error = nil;
    }
}
@end

2.RKObjectManagerにサブクラスを登録する

[objectManager registerRequestOperationClass:[EPObjectRequestOperation class]];
于 2013-03-11T16:07:24.470 に答える