使用 POST 发送 JSON 到 Restful Web 服务
我想用 POST/PUT 发送 JSON 我不知道它是否是一样的想法。 这是 json 。 {"author":"mehdi","email":"[电子邮件受保护]","消息":"Hello2"} 这是我的沃德尔。
<resource path="/messages/"><method id="get" name="GET"><request><param name="start" style="query" type="xs:int" default="0"/><param name="max" style="query" type="xs:int" default="10"/><param name="expandLevel" style="query" type="xs:int" default="1"/><param name="query" style="query" type="xs:string" default="SELECT e FROM Message e"/></request><response><representation mediaType="application/xml"/><representation mediaType="application/json"/></response></method><method id="post" name="POST"><request><representation mediaType="application/xml"/><representation mediaType="application/json"/></request><response><representation mediaType="*/*"/></response></method><resource path="{keyid}/"><param name="keyid" style="template" type="xs:int"/><method id="get" name="GET"><request><param name="expandLevel" style="query" type="xs:int" default="1"/></request><response><representation mediaType="application/json"/></response></method></resource></resource>
当我尝试使用 netbeans“测试静态 Web 服务”发布时,它可以工作,这就是 http 监视器显示的内容
状态:201(已创建)
时间戳:2011 年 5 月 21 日星期六 20:30:33 格林威治标准时间
已发送: {"author":"mehdi","email":"[电子邮件受保护]","message":"Hello2"}
已收到:
<小时>请求:POST http://localhost:8080/TRESTful/resources/messages/? 时间戳=1306009225785
状态:201(已创建)
时间戳:2011 年 5 月 21 日星期六 20:20:25 格林威治标准时间
已发送: {"author":"mehdi","email":"[电子邮件受保护]","message":"Hello2"}
但现在我不知道怎么用ASIHttpRequest .首先我应该做 [request setRequestMethod:@"PUT"]; ?我应该像字符串一样发送所有 json,还是将每个值与嘿键一起放置,如下所示:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"author"];
[request setPostValue:@"Copsey" forKey:@"email"];
[request setPostValue:@"Hello2" message:@"author"];
[request startSynchronous];
或者我应该将 JSON 字符串放入一个值中?
请帮忙。
i want to send JSON with POST/PUT i don't know if it's the same think .
This is the json .
{"author":"mehdi","email":"[email protected]","message":"Hello2"}
this is my wadl .
<resource path="/messages/"><method id="get" name="GET"><request><param name="start" style="query" type="xs:int" default="0"/><param name="max" style="query" type="xs:int" default="10"/><param name="expandLevel" style="query" type="xs:int" default="1"/><param name="query" style="query" type="xs:string" default="SELECT e FROM Message e"/></request><response><representation mediaType="application/xml"/><representation mediaType="application/json"/></response></method><method id="post" name="POST"><request><representation mediaType="application/xml"/><representation mediaType="application/json"/></request><response><representation mediaType="*/*"/></response></method><resource path="{keyid}/"><param name="keyid" style="template" type="xs:int"/><method id="get" name="GET"><request><param name="expandLevel" style="query" type="xs:int" default="1"/></request><response><representation mediaType="application/json"/></response></method></resource></resource>
When i trie to post with netbeans "Test restful webservice " it work and this is wat the http monitor show
Status: 201 (Created)
Time-Stamp: Sat, 21 May 2011 20:30:33
GMTSent:
{"author":"mehdi","email":"[email protected]","message":"Hello2"}Received:
Request: POST
http://localhost:8080/TRESTful/resources/messages/?
timestamp=1306009225785Status: 201 (Created)
Time-Stamp: Sat, 21 May 2011 20:20:25
GMTSent:
{"author":"mehdi","email":"[email protected]","message":"Hello2"}
But now i dont know how it with ASIHttpRequest .first should i make [request setRequestMethod:@"PUT"]; ? and should i send all the json like a string , or put each value with hey key , like this :
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"author"];
[request setPostValue:@"Copsey" forKey:@"email"];
[request setPostValue:@"Hello2" message:@"author"];
[request startSynchronous];
or should i put the JSON string in one value ?
Help please .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想发送 json,你有两个选择:
a) 在文件中发送
- 如果 json 中有大量数据并且您可能希望将其存储在服务器上以供以后解析,请使用此选项。
b) 将其作为一个值中的字符串发送
-如果您的 json 很短并且您想立即解析它,请使用此
编辑
c) 如果您想跳过服务器上的解析并直接访问数据,请将其作为单独的值发送。如果数据很少,则应使用此方法。
Well if you want to send a json you have two options:
a) Send it in a file
-Use this if you have a lot of data in a json and you may want to store it on the server for later parsing.
b) Send it as string in one value
-Use this if your json is short and you want to parse it immediately
EDIT
c) Send it as seperate values if you want to skip parsing on the server and access data directly. This should be used if there is few data.