2

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 Answers 3

1
<?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

Sign up to request clarification or add additional context in comments.

2 Comments

I was trying this but I'm getting error Warning: http_build_query(): Parameter 1 expected to be Array or Object. I included $headers = array();
$options = array( 'method' => 'POST', 'data' => $jsonData, 'timeout' => 15, 'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'), ); $result = drupal_http_request($request_url, $options);
1

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']));

6 Comments

But in this case its saved in the database with a '+'. Is there any way to avoid that?
Where does the + come from ?!
@MuhammadReda Node.js application logic most probably. Sreekesh, you're going to need to alter this on the Node.js side of things.
When I input name as 'sreekesh okky' its send to the node as 'sreekesh+okky'
@SreekeshOkky Perhaps then you should splitup the parameters, or stil better use JSON, instead of relying on GET.
|
0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.