When you create a service endpoint you can enable various request parsers (application/json, application/x-www-form-urlencoded, multipart/form-data...) How should I decide whether to send the request using JSON vs form-data?

Most of the "node create" examples I've seen do something like this (I think this uses form-data):

    //create a node
    $node_data = array(
                      "title"=>"DHC Tool 01",

                      "status"=>false, //ensure unpublished
                      "language" => "und", //lanugage neutral


  // Use JSON
  // $node_data = '{"title":"Tool Unpublished 10","type":"item","status":false,"language":"und","field_webpage":{"und":[{"url":"http:\/\/berkeley.edu"}]}}';
    $options['data'] = http_build_query($node_data, '', '&');
    $response = drupal_http_request($base_url . '/node', $options);

Is there any advantage to sending the node create request using JSON?

How would the above code be modified to use JSON?


  • I have voted to remove my answer that said hooks do not run with JSON. Apparently the conversion between JSON and Drupals internal format is easy enough to do. groups.drupal.org/comment/reply/199748/659523 Is correct.
    – Letharion
    Jan 4, 2012 at 12:42

1 Answer 1


This code will send a request encoded as JSON:

function toolreq_test_create() {
  $base_url = 'http://example.com/dirt';
  $options = toolreq_service_login(); //this yields $options['headers']['Cookie'] = $data->session_name . '=' . $data->sessid;
  if (!is_array($options)) {
    print "error<p>";

   //create a node
   $node_data = array(
     'title' => 'DHC Tool 03',
     'type' => 'item',
     'status' => FALSE, //ensure unpublished
     'language' => 'und', //lanugage neutral
     'field_webpage' => array(
       'und' => array(
         0 => array('url' => 'http://berkeley.edu',)

   //use json
   $options['headers']['content-type'] = "application/json";
   $options['method'] = 'POST';
   $options['data'] = json_encode($node_data); 
   $request = drupal_http_request($base_url . '/node', $options);
   if ($request->error) {
     print ("Error doing http request");
     print '<pre>' . var_dump($request) . '</pre>';
   else {
     print ("Success doing http request");
     print '<pre>' . var_dump(json_decode($response->data)) . '</pre>';

When considering the best format for your request keep in mind:

  • If I send status=>false as x-www-form-urlencoded, the resulting node is not "unpublished" as I wish. (The content-type's default is "published" and the requirements don't allow me to change that.) If I run the array through json_encode(), I can force the node to be "unpublished." This worked on my local stack but failed when I uploaded it to the server. For more on this help.getpantheon.com/pantheon/topics/…
    – Brian Wood
    Jan 3, 2012 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.