tags:

views:

328

answers:

4

When you use REST to create a new entry using a POST, I see some APIs such as Google's specify that you send XML as part of the request while others specify that you send key/value pairs. Is there a standard or best practice for REST POST-s?

+3  A: 

Any representation format that works is okay, with the stipulation that you should try very hard to use standard formats such as Atom where they exist.

Update Here's a relevant quote from Roy Fielding (co-author of the HTTP standard, and the person who articulated REST in his PhD dissertation). How you design the representations used in your web service is of central importance:

A REST API should spend almost all of its descriptive effort in defining the media type(s) used for representing resources [...]

Be sure to read the follow-on Q&A.

Jim Ferrans
A: 

SOAP is the standard for WebServices (I think you're slightly confused bewteen WebServices and REST).

But it's really up to the implementors.

Noon Silk
Feel free to explain what is wrong with this post ...
Noon Silk
"Web Services" is now a more general term describing ways to structure the Web so that it's as convenient for programs to browse and operate on as it is for humans. SOAP is a set of W3C standards that have this goal, and SOAP uses XML exclusively. REST is not a standard per se, but an architectural style for structuring web services that maximally leverages HTTP and other web standards. Richardson and Ruby's *RESTful Web Services* (O'Reilly) is a wonderful resource for learning about the REST approach.
Jim Ferrans
Yes ... but purely having a 'WebService' doesn't mean it's REST. REST is more than that. But this guy doesn't actually want full REST, he wants only a standard for WebServices. Hence my answer.
Noon Silk
When you use the phrase "Web Service" with capitalization, it is assumed that you are talking about SOAP, WSDL etc. This has nothing to do with REST and therefore nothing to do with the question. Where do you get the impression that the OP "doesn't actually want full REST"?
Darrel Miller
Because he references the Google API, which is not 'full' REST, as it says right on their page: "This use of HTTP requests for a publishing and editing protocol is in the spirit of the REST approach to web service interfaces.". It's "in the spirit", but not the same.
Noon Silk
+1  A: 

It depends on the REST service implementer.

If the REST service is an adaptation of an existing html HTML form post key value pairs are generally easier to start off with.

When posting information from JavaScript it is usually easier to use JSON.

XML is used often because it is easy for humans to understand and there are heaps of tools in every language/platform that can deal with it.

leonm
+1  A: 

I suggest using what is simplest because that is what REST is all about. The code snippet below is how I do a post. I know you were not looking for code specifically, but the API below (httpClient) works great. Then you decode it by using the tools we coders have always used (request.getParameter()). I believe this is what sets REST apart from SOAP. Don't make it difficult! Use HTTP!

    public void testHttpClient() {
 PostMethod pMethod = null;
 pMethod = new PostMethod("...url...");
 NameValuePair[] data = {new NameValuePair("activeFlag", "yes"), new NameValuePair("custCode", "B010"), new NameValuePair("comments", "mark is cool")};
 pMethod.setRequestBody(data);
 this.makeCall(pMethod);
}
private String makeCall(HttpMethod method) {
 String response = null;
 HttpClient client = new HttpClient();
 client.getParams().setAuthenticationPreemptive(true);
 client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.logon, this.pass));
 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
 method.getParams().setIntParameter(HttpMethodParams.SO_TIMEOUT, 5000);
 try {
  int statusCode = client.executeMethod(method);
  if (statusCode != HttpStatus.SC_OK) {
   System.err.println("Method failed: " + method.getStatusLine());
  }
  String aLine = null;
  StringBuffer sb = new StringBuffer();
  BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
  while ((aLine = in.readLine()) != null) {
   sb.append(aLine.trim());
   System.out.println(aLine);
  }
  in.close();
  response = sb.toString();
 } catch (HttpException e) {
  System.err.println("Fatal protocol violation: " + e.getMessage());
  e.printStackTrace();
 } catch (IOException e) {
  System.err.println("Fatal transport error: " + e.getMessage());
  e.printStackTrace();
 } finally {
  method.releaseConnection();
 }
 return response;
}
sliver