开发者

Post Data format is diffrent then what I needed

开发者 https://www.devze.com 2023-03-31 15:12 出处:网络
I am having a simple problem while making a request to server for updating a name field. I need to post some data in this format:-

I am having a simple problem while making a request to server for updating a name field. I need to post some data in this format:-

   {"api_token"=>"api", "device_token"=>"device", "user"=>{"name"=>"mohit"}, "id"=>"4"} 

But when i am trying to post something its posting in this format:-

  {"user"=&g开发者_StackOverflow社区t;"(\n        {\n        name = ChangeName;\n    }\n)", "api_token"=>"api", "device_token"=>"device", "id"=>"4"}

I am not able to figure out how to change my code to generate proper request. Here is the code that I am using.

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/users/4?api_token=api&device_token=device"]];
NSMutableDictionary *dict= [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Mike",@"name", nil];
NSArray *array=[[NSArray alloc]initWithObjects:dict, nil];
        [request setPostValue:array forKey:@"user"];
         [request setRequestMethod:@"PUT"];
        [request setDelegate:self];
        [request setDidFinishSelector:@selector(requestFinished:)];
        [request startAsynchronous];

Please let me know if i need to post some more code fragments.


ASIFormDatRequest setPostValue:forKey: wants strings, not structures. It ends up calling description to convert them to strings and you're getting the printable description of an array with a dictionary in it.

Rails uses a naming scheme that allows you to simulate a hierarchy in a flat space using a field naming convention detailed at http://guides.rubyonrails.org/form_helpers.html. You should read that and understand the html produced by the form helpers.

Try:

[request setPostValue:@"mohit" forKey:@"user[name]"];

and rails will unpack it into the proper kind of collection on the server.

0

精彩评论

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