Send Data from Drupal to node.js

313 Views Asked by At

I'm a newbie with drupal.

I was trying to send data from drupal to node.js function and save the data from node into mongo.

I wrote a function in php

function sample_working_submit($form, &$form_state) { //Function to connect with node and save into Mongo using node
  $Name = check_plain($form_state['values']['Name']);
  $Age = check_plain($form_state['values']['Age']);
  $request_url = "http://10.20.5.112:3001/save/$Name/$Age ";
  $response = drupal_http_request($request_url);
}

This is working as long as there is no 'space' between the names(input). How can save the input with spaces.Why does this issue came?

How can i rewrite the function as post?

3

There are 3 best solutions below

2
On BEST ANSWER
<?php
$url = http://localhost:8080/myservlet;

$data = array (
    'name' => check_plain($form_state['values']['Name']),
    'age' => check_plain($form_state['values']['Age'])
    );

$response = drupal_http_request($url, $headers, 'POST', json_encode($data));
?>

This would POST data to URL. Note that you need to change logic in server side as well to receive POST data instead of GET data.

Refer here for more info

0
On

For POST requests I use SimpleBrowser instead of drupal_http_request(). It's easy to use and you'll be able to pass strings in any form.

6
On

If the space between the names is the issue, try using urlencode().

The code will be something like:

$Name = urlencode(check_plain($form_state['values']['Name']));