开发者

Parameters repeated while using NSURL to create new post on rails server using JSON

My issue is that when i POST to a rails server to create a new post using the following code from the iphone simulator, the parameters are sent twice.

NSDictionary *thestuff = [NSDictionary dictionaryWithObjectsAndKeys:
                         titleIs, @"title", 
                         descIs, @"description", nil];
NSString *tojson = [thestuff JSONRepresentation];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request addValue:@"form-data" forHTTPHeaderField:@"Content-Disposition"];
[request setHTTPBody:[tojson dataUsingEncoding:NSUTF8Stri开发者_高级运维ngEncoding]];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

Assuming NSString tojson contains this :

    {"title":"wwd","description":"wwwwwww"}

when i send the request, the rails server shows parameters as:

Processing by PostsController#create as JSON
Parameters: {"title"=>"wwd", "description"=>"wwwwwww", "post"=>{"title"=>"wwd", "description"=>"wwwwwww"}}

as you can see, the title and description are sent twice according to this. Is there a way to fix this and does this pose a security issue if i use Post to post username and password? The post is created as I want without any issues but i really want to fix this double parameter post.


Ideally this expected behavior which can be changed depending on your needs. This is enabled by a property called ParamsWrapper in ActionController and you can read the documentation here.

It is enabled default to make it easier to give wrapped parameters to Create and Update actions on ActiveRecord objects.

You can disable this by changing it in

config/initializers/wrap_parameters.rb

wrap_parameters format: [:json] if respond_to?(:wrap_parameters)

to false

wrap_parameters false

read about other option available on ruby docs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜