send JSON with POST to Restful web service
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@hotmail.fr","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 GMT
Sent: {"author":"mehdi","email":"email@hotmail.fr","message":"Hello2"}
Received:
Request: POST http://localhost:8080/TRESTful/resources/messages/? timestamp=1306009225785
Status: 201 (Creat开发者_Go百科ed)
Time-Stamp: Sat, 21 May 2011 20:20:25 GMT
Sent: {"author":"mehdi","email":"email@hotmail.fr","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 .
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.
精彩评论