我在使用 AFJSONOperationRequest 之前使用 NSMutableURLRequest,但在我的 Rails 应用程序中获取数据时遇到问题。
如果我使用:
NSMutableURLRequest *request = [[HTTPClient sharedClient] multipartFormRequestWithMethod:@"POST" path:path parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
{
}];
然后 :
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
else {
}
}
我在 Rails 日志中正确格式化了我的数据:
Parameters: {"contact"=>{"country_id"=>"45", "lastname"=>"Tutu"}}
但我不需要 AFMultipartFormData 来发送文件......(没有文件要发送)
所以,相反,我使用:
NSMutableURLRequest *request = [[HTTPClient sharedClient] multipartFormRequestWithMethod:@"POST" path:path parameters:dict];
我的 AFJSONRequestOperation 也是。但是,我的参数现在没有在我的 rails 应用程序中正确设置:
Parameters: {contact[country_id] => "45", contact[lastname] => "Tutu"}
代替
Parameters: {"contact"=>{"country_id"=>"45", "lastname"=>"Tutu"}}
我不明白为什么。当我不使用块时,看起来请求的主体设置不正确:“constructingBodyWithBlock”。