Charles Map Local不适用于JSON数据?

时间:2014-02-17 12:44:22

标签: ios json mocking charles-proxy

我正在尝试使用Charles模拟(模拟它)服务器。我发现Charles有一个名为“Map Local ...”的东西,它允许我使用本地存储的文件回复客户端。

通过右键单击Charles Sequence列表并选择“Save Response ...”来存储我指向的文件。

但是,在使用我的iOS应用程序测试时,我收到以下错误消息:

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

所以不知怎的,我没有发送带有Content类型的正确头信息的响应。有没有办法告诉Charles,响应是JSON?可能的问题是存储的文件只包含JSON数据而不包含任何标题。

2 个答案:

答案 0 :(得分:37)

您需要在Charles中添加重写规则,以将Content-Type标头更改回application / json。

Map Local只会提供text / plain Content-Type。

要执行此操作,请转到工具>重写......>添加。

添加您在本地映射的所有位置,然后为规则添加如下所示的位置:

rewrite rule

我绝不会建议只为Charles调整代码,因为如果您无法完全复制它们,这会破坏测试Web服务的重点。

答案 1 :(得分:0)

我知道你在问如何告诉Charles将响应视为JSON,但另一方面也有另一种解决方案。

我可以看到你正在使用AFNetworking。您的请求预计会收到"text/json", "application/json", "text/javascript",但您的文件为text/plain,因此我们需要对此进行更改。  在构建AFHTTPRequestOperation时,您可以使用以下代码行设置acceptableContentTypes operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

所以整个代码看起来像这样

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];