Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Charles Map Local not working for JSON data?

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.

like image 457
Erik Engheim Avatar asked Feb 17 '14 12:02

Erik Engheim


People also ask

How do I map a local response in Charles proxy?

json on my Desktop. Map Local: Now we go back to Charles map local and clicking Add and we put the host we want to switch-out from, the path, whether it's http or https. [Time-Saver: If you just paste the whole URL into host it will auto-populate the relevant fields for you.] Then in Map To you want to define your .

How do you mock response to Charles?

You can choose a URL that contains the desired response or either create a mock one (by using tools such as "mocky.io"). 2) You can use "Map Remote" and do the same - choose the network you would like to modify with the URL that contains the response you desire.


1 Answers

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.

like image 67
liamnichols Avatar answered Sep 23 '22 22:09

liamnichols