zyxel
zyxel

Reputation: 1094

json response is interpreted as text/plain

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?

Upvotes: 11

Views: 5594

Answers (2)

Stefano Mondino
Stefano Mondino

Reputation: 1219

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).

Upvotes: 20

Marc B
Marc B

Reputation: 360612

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.

Upvotes: 1

Related Questions