Thursday, October 18, 2012

Drupal 7, Services 3.x , Request and Response

Here is something that I just recently learned regarding request/response from a REST server.
In this case, I am using Drupal 7 with Services 3.x.

It's just one of those things I guess you don't lean until you need to, and I never had yet.  It was difficult to find the info though.

Here is the scenario:

  1. You are calling a web service using JSON as the Content Type.
  2. You are getting back an XML response.
  3. You want to get back a JSON response.

Here's what you need to do.
Goto your services
 
Select "Edit Server"

Make sure the response formatter "json" is selected.  It may already be.


Now in your JSON REQUEST, add the following header:
Accept = "application/json"

For some reason, Drupal was returning the response in XML format by default, I don't know why, but adding this header to the request immediately gave me back a JSON response.