11

I have the following code:

NSURL *URL = [NSURL URLWithString:[@"some-address"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];

RKObjectRequestOperation *requestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:[self.objectManager responseDescriptors]];

[requestOperation start];
[requestOperation waitUntilFinished];

I get the following error.

Object request failed: Underlying HTTP request operation failed with error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
    "application/x-www-form-urlencoded",
    "application/json"
)}, got text/plain" UserInfo=0x1f5e3c40 {NSLocalizedRecoverySuggestion={"total_rows":16,"offset":1,"rows":[

{"id":"1","key":1,"value":{"_id":"1","_rev":"1-e75042683867a7030fc4d3aa3b72ef35",
"user":{
"userId":"1",
"name":"A",
.......
]}}, .....

Why I get this error, when the response is in Json format?

Marc B
  • 356,200
  • 43
  • 426
  • 500
zyxel
  • 1,084
  • 3
  • 15
  • 23

2 Answers2

20

we did it. just set

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"text/plain"];

and feel free to change RKXMLReaderSerialization class with RKNSJSONSerialization class if you're using JSON instead of XML (XML was our case).

Stefano Mondino
  • 1,219
  • 11
  • 15
1

You didn't set the mime-type header properly in your response. Note the error says got text/plain, while the code is expecting either application/json or application/x-www-form-urlencoded.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • what if MIME TYPE (server side) is not under my control? I'm using Restkit 0.20 – Stefano Mondino Jan 17 '13 at 18:06
  • then see if you can override/add the client-side acceptable mime-types – Marc B Jan 17 '13 at 18:07
  • already done. I've used this : [RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"text/plain"]; but it keeps on failing object mapping. Every network request/response works fine in all my app except this one. We've notified "server guys" about this bug (there's no reason to set a text/plain mimetype when you're actually sending a real, well formatted XML) but they won't fix it. There must be something inside the XML Parser (which is NOT Restkit's anymore since 0.20), maybe I should open another topic for this (nru asked about JSON) – Stefano Mondino Jan 17 '13 at 18:16