发布数据格式与我需要的不同 [英] Post Data format is diffrent then what I needed

查看:97
本文介绍了发布数据格式与我需要的不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在向服务器发出更新名称字段的请求时,我遇到了一个简单的问题。我需要以这种格式发布一些数据: -

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"=>"(\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:想要字符串,而不是结构。它最终调用描述将它们转换为字符串,并且你得到一个带有字典的数组的可打印描述。

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使用一个允许你的命名方案使用 http://guides.rubyonrails.org/上详细说明的字段命名约定来模拟平面空间中的层次结构form_helpers.html 。你应该阅读并理解表单助手产生的html。

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.

尝试:

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

并且rails会将其解压缩到服务器上的正确类型的集合中。

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

这篇关于发布数据格式与我需要的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆