17

I am trying to simulate (mocking it) a server using Charles. I found out that Charles has something called "Map Local..." which allows me to reply to a client using files I have stored locally.

The files I am pointing to have been stored by right clicking in the Charles Sequence list and choosing "Save Response...".

However when testing this with my iOS app I get the following error message:

Failed to get areas: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/plain" 

So somehow I am not sending the response with the correct header information for Content type. Is there a way of telling Charles that the response is JSON? A possible problem is that the files stored only contains the JSON data and not any header.

Erik Engheim
  • 8,182
  • 4
  • 36
  • 51
  • 1
    Know nothing of the tool, but make sure your file has a .json suffix. If that doesn't do it you need to figure out how to set the mime type. – Hot Licks Feb 17 '14 at 12:53

2 Answers2

41

You need to add a rewrite rule in Charles to change the Content-Type header back to application/json.

Map Local will only give a text/plain Content-Type.

To do this, go to Tools > Rewrite... > Add.

Add all the locations that you are mapping locally and then for the rules, add one that looks like the following:

rewrite rule

I would never recommend adjusting your code just for Charles as this defeats the point of testing your web services if you can't replicate them exactly.

liamnichols
  • 12,419
  • 2
  • 43
  • 62
  • Thanks worked like a charm. I had no idea Charles was this powerful. This rewrite function gives so many possibilities. – Erik Engheim Feb 18 '14 at 14:59
  • @AdamSmith Yep! Its not very well documented but once you get your head around all the features it is awesome! – liamnichols Feb 18 '14 at 15:32
  • I just mapped a local file that is named "localFile.json" and got `"Content-Type" = "application/json"` in the response in my test... I got here looking for a way to modify the headers of the response though, no luck yet. – manroe Jun 24 '16 at 22:13
  • Charles will add header values appropriate to the file type you are using for map local. If you want `application/json` for `Content-Type` then make sure the mapped file is of type `.json`. This will prevent you from having to create an additional rewrite. – Christopher Moore Feb 03 '17 at 12:54
0

I know you are asking how to tell Charles to treat response as JSON but on the other hand there is also another solution.

I can see you are using AFNetworking. Your request is expecting to receive "text/json", "application/json", "text/javascript" but your files are text/plain so we need to change this. While building AFHTTPRequestOperation you can set acceptableContentTypes with this line of code operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

So whole code can look like this

AFHTTPRequestOperation* operation = [manager HTTPRequestOperationWithRequest:req success:^(AFHTTPRequestOperation *operation, id responseObject) {

   //success

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

  //failure
 }];

operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
[operation start];
lvp
  • 2,078
  • 18
  • 24