views:

508

answers:

3

I see APIs such as PayPal, etc. offering to call their services using NVP or SOAP/WSDL. When using a .NET environment (3.5) using traditional web services (no WCF) which is better and why? I know WSDL lets you drop in the API URL and it generates the wrappers for you. So then why do companies even offer NVP?

A: 

I assume that by Name Value Pairs, you mean REST services.

The benefits to REST are primarily ease of development, simplicity and elegance, and lower overhead (which is very important if you are sending and receiving a lot of small messages).

Here are some of the advantages of REST:

  • REST is more lightweight
  • Human readable results
  • Everything is a URI addressable resource
  • REST services are more easily cached
  • REST is easier to build (no toolkits are required)
  • REST is easier to call (HTTP - GET, POST, PUT, DELETE)
Gabriel McAdams
Here's the thing. The first time I used an API (It was Facebook in particular), I hand coded all the wrappers. My boss comes to me and says why did you do this when you could have just used the WSDL! Because that way a) You don't hand code any of the wrappers and b) if their API changes, you don't need to go back through and change all your wrappers and therefore c) your application doesn't break on an API change by the vendor
CoffeeAddict
First of all, Facebook isn't likely to make big breaking changes to their API. Second, you should write more flexible wrappers (see: this code example using the Bing API - http://msdn.microsoft.com/en-us/library/dd251093.aspx)
Gabriel McAdams
Ian Clelland
@ian: I think you need to read this page: http://en.wikipedia.org/wiki/REST#RESTful_web_services (and this page: http://wiki.developers.facebook.com/index.php/API#REST_Interface)
Gabriel McAdams
@coffeeaddict In many ways your boss is right. The way the Facebook API is written, you have no choice but to create wrapper classes that couple you in the same way as a WSDL client proxy does. If the Facebook API were actually RESTful the coupling would be significantly reduced and the benefits of using it over using WSDL would be much more apparent.
Darrel Miller
@Gabriel: The PayPal API that was specifically mentioned uses the encoded POST body (rather than the URI or message headers) to include details like the resource which is being acted on, the action being performed (inquiry vs update) and all authentication information. The API has a single URI used for all requests. Aside from being carried over HTTP, it has essentially no aspects of a RESTful web service.
Ian Clelland
@Ian: HTTP is REST. Using the POST body is RESTful. Can you explain a little bit what you feel is missing?
Gabriel McAdams
@Gabriel: In that case, SOAP is RESTful, too :) What I would say is missing from the PayPal API is: different URIs for different resources; using GET for inquiries, rather than POST; returning URIs in message responses (discoverable URI-space); and (possibly even) using HTTP headers for authentication, rather than the POST body.
Ian Clelland
Gabriel, what do you mean by more flexible wrappers. How do you know I'm not going to be coding what you say is a "flexible wrapper". I don't get why you stated this.
CoffeeAddict
Darrel, my boss values using WSDL at all times. If I explained to him NVP and RESTful he'd still come back and argue that the WSDL prevents breaking in your code and since .NET automatically generates the classes, all we have to do is create wrappers. If I went and created my own wrappers using Name VAlue pairs, he's throw a fit like a baby.
CoffeeAddict
PayPal has both NVP and SOAP
CoffeeAddict
I still have not gotten a clear answer to my question in terms of my boss arguing to always use WSDL because it generates all proxy classes for you and all you have to do is to then start creating wrapper classes.
CoffeeAddict
1) Performance (NVP is smaller). Depending on how many calls you make, it could be huge. 2) Your boss is worried about WSDL being generated, explain to him that NVP service requires only a simple URI call. SOAP is harder to develop and maintain than NVP. 3) NVP can be Asynchronous. 4) NVP using WCF can be type-safe (see http://www.developer.com/net/article.php/10916_3695436_1/Creating-RESTful-Web-Services-with-Windows-Communication-Foundation.htm and http://msdn.microsoft.com/en-us/library/ee391967.aspx and http://msdn.microsoft.com/en-us/netframework/dd547388.aspx)
Gabriel McAdams
3) NVP can be Asynchronous - yea but as of .NET 2.0 web services period are asynchronous even for SOAP WSDL
CoffeeAddict
A: 

NVP is HTTP POST

name=fred
amount=100
code=403

etc

This is the default format from any HTML browser so it's simple to implement for sending data to a web service

I don't think it's a good format for receiving data from web service? JSON or XML would be more suitable

No everyone uses VisualStudio, or has access to automatic wrapper generators, or wants to use such a beast

Many web mashups are coded in Javascript, so using HTTP POST to send data is the simplest way. The return result is a standard HTML response code (200, 403, 500, etc) and/or some JSON

Many service providers offer multiple API's to cater for all customers

TFD
+10  A: 

There seems to be never-ending confusion in this industry about the different types of web services.

SOAP is a messaging protocol. It has as much in common with REST as an apple has with a lawn tractor. Some of the things you want in a messaging protocol are:

  • Headers and other non-content "attributes."
  • Addressing - routing of a message to different servers/recipients based on the headers;
  • Guaranteed delivery via queuing and other methods;
  • Encryption, signing, and other security features;
  • Transactions and orchestrations;
  • Accurate representation of complex structured data in a single message;

...and so on. This is not an exhaustive list. What WSDL adds to SOAP, primarily, is:

  • Discoverability via a contract, a form of machine-readable "documentation" that tells consumers exactly what is required in order to send a message and allows proxies to be auto-generated;

  • Strict, automated schema validation of messages, the same way XSD works for XML.

REST is not a messaging protocol. REST is a system of resources and actions. It is a solid choice for many architectures for several important reasons as outlined by other answers. It also has little to no relevance to "NVP" services like PayPal and flickr.

PayPal's NVP API is not REST. It is an alternative, RPC-like messaging protocol over HTTP POST for clients that don't support or have difficulty supporting SOAP. This isn't my opinion, it's a statement of fact. One of the fields in the NVP is actually METHOD. This is clearly RPC verbiage. Take a look at their API for UpdateRecurringPaymentsProfile and try to tell me that this makes a lick of sense to describe as a "resource". It's not a resource, it's an operation.

In the case of PayPal specifically, the "NVP" (HTTP POST) API is inferior to the SOAP API in almost every way. It is there for consumers who can't use SOAP. If you can use it, you definitely should.

And I'm not necessarily bashing PayPal for this, either. I know a lot of folks have bashed them for not putting together a "proper" RESTful API but that is not what I am getting at. Not every service in the world can be accurately described with REST. PayPal isn't really a resource-based system, it's a transactional system, so I can forgive their architects and developers for not having a perfectly elegant REST architecture. It's debatable perhaps, but it's not black-and-white. It's fine; I'll just use the SOAP system if I need to.

Compare this to, say, the Twitter API. This is a true REST service. Every "operation" you can perform on this API is accurately described as either the retrieval or submission of a particular kind of resource. A resource is a tweet, a status, a user. In this case it literally makes no sense to use a complex SOAP API because you're not really sending messages, you're not performing transactions, you're just asking for specific things, and these things can be described with a single URL. The only difference is that instead of getting an HTML web page back, you're getting some XML or JSON data; the way you request it is exactly the same.

A REST Web Service usually (always?) uses HTTP GET for the retrieval of some resource. And Twitter does exactly this. GET still uses "Name-Value Pairs" - that's the query string, ?q=twitterapi&show_user=true. Those bits after the ? are name-value pairs. And here's a great example of why you would want to use REST over SOAP; you can hook this up to an RSS feed and get streaming updates. I can turn it into a Live Bookmark in Firefox. Or I can download it in JSON format and bind it to something like a jqGrid. The interesting thing is not that the request uses "Name-Value Pairs"; the interesting thing is that it's a simple URL and can be consumed by anything that knows how to request a web page.

So to try and summarize all of what I've said, think of it this way:

  • Use a REST API (if available) when you want to expose data, or consume or publish it, as a permanent resource.

  • Use a SOAP API when the system is transactional in nature and/or when you need the advanced features that a complex messaging protocol can offer, such as RM and addressing.

  • Use an RPC API (which includes just about any API that's modeled entirely around HTTP POST) when there is no SOAP API or when you are unable to use the SOAP API.

Hope that clears up some of the confusion.

Aaronaught
Nice explanation/detail - IMO your spot on.
Mr-sk
nice, thanks much!
CoffeeAddict