Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json response is interpreted as text/plain

Tags:

restkit

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?

like image 791
zyxel Avatar asked Jan 17 '13 16:01

zyxel


People also ask

Is JSON sent as plain text?

The Javascript Object Notation(JSON) is used to transfer or exchange information on the internet. JSON is just the plain text written as a javascript object.

What is the type of JSON response?

Valid Data Typesa string. a number. an object (JSON object) an array.

What is JSON response?

JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and transporting data. JSON is often used when data is sent from a server to a web page.

How does JSON send data in response?

Send JSON Data from the Client SideUse JSON. stringify() to convert the JavaScript object into a JSON string. Send the URL-encoded JSON string to the server as part of the HTTP Request. This can be done using the HEAD, GET, or POST method by assigning the JSON string to a variable.


2 Answers

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

like image 184
Stefano Mondino Avatar answered Oct 06 '22 22:10

Stefano Mondino


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.

like image 29
Marc B Avatar answered Oct 06 '22 21:10

Marc B