服务器端无法读取帖子正文请求

时间:2018-08-02 07:08:23

标签: objective-c afnetworking

image from postman 我创建了一个发布请求,并将此正文作为字符串发送到服务器:

contacts:[
  {
    "company_name" : "rrrr",
    "contact_lname" : "rrrr",
    "remark" : "Fdfdfdfffdfd",
    "Images" : [
      "http:\/\/otrackapi.omegasoftware.ca\/ActStaff\/public\/uploads\/user-image-115.jpg"
    ],
    "type_id" : "24",
    "contact_phone" : "1212233332",
    "lng" : "-122.041202",
    "lat" : "37.337566",
    "contact_email" : "www@test.com",
    "contact_fname" : "rrrr"
  }
]

在以下使用AFnetworking的请求中:

 NSError *error;
     NSData *leadData = [NSJSONSerialization dataWithJSONObject:body options:NSJSONWritingPrettyPrinted error:&error];
    NSString *leadString = [[NSString alloc] initWithData:leadData encoding:NSUTF8StringEncoding];
    NSString *leadTest = [NSString stringWithFormat:@"%@%@",@"contacts:",leadString];
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:parameters  error:&error];
    request.allHTTPHeaderFields = parameters;
    request.timeoutInterval = 30;
    [request setHTTPBody:[leadTest dataUsingEncoding:NSUTF8StringEncoding]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        NSHTTPURLResponse *myResponse = (NSHTTPURLResponse *)response ;
        if (block) {
            block(error, responseObject);
        }else {
            block(responseObject, error);
            return ;

        }
    }]resume];

但是有一个错误,服务器无法读取正文,我收到以下responseObject:

response =     {
        exception = BadRequestException;
        message = "The contacts field is required.";
    };

我与后端人员进行了交谈,他们告诉我服务器未读取该正文。 任何人都知道发生了什么事吗?有帮助吗?

更新:我添加了一个邮递员的图像,它以这种方式工作,如何在目标c中转换它? 非常感谢

1 个答案:

答案 0 :(得分:1)

{
"contacts": [{

    "company_name": "rrrr",
    "contact_lname": "rrrr",
    "remark": "Fdfdfdfffdfd",
    "Images": [
        "http:\/\/otrackapi.omegasoftware.ca\/ActStaff\/public\/uploads\/user-image-115.jpg"
    ],
    "type_id": "24",
    "contact_phone": "1212233332",
    "lng": "-122.041202",
    "lat": "37.337566",
    "contact_email": "www@test.com",
    "contact_fname": "rrrr"

}]    }

您的JSON无效。请尝试这个。