0

I want to send a simple HTTP POST request from Android to a Node.js server. Can anyone tell me what I'm doing wrong?

Android code:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://54.68.139.250/bscreateuser");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", "hellomoto"));
        nameValuePairs.add(new BasicNameValuePair("password", "pswd"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Node.js code:

app.get('/bscreateuser', function(request, response) {
  process.stdout.write("Attempted to create a user.");
  bscreateUser(request.query.username, request.query.password);
  response.send('-- BS -- User Created! username: ' + request.query.username +
    ' password: ' +
    request.query.password);
});

function bscreateUser(username, password) {
  messageBody = 'create_user("' + username + '","' + password + '")';
  queueUrl = DAO_QUEUE_URL;
  // sys.puts("--- going for BS ---");
  sendSQSMessage(JSON.stringify(messageBody), queueUrl);
}

2
  • 1
    It appears that some people were able to answer your question without very much detail. In the future though, please include a detailed description of the behavior you're observing, rather than just describing what you're trying to do. Descriptions of undesired behavior are usually necessary to be able to help you. Commented Feb 17, 2015 at 6:14
  • I agree with @skrrgwasme. The question does not post the exact error that occurs. The answers were based on the most obvious mistake on the server side code that you have provided. Commented Feb 17, 2015 at 6:30

2 Answers 2

3

In the node.js code, you are using

app.get('/bscreateuser', ...

Instead, you need to use the post method

app.post('/bscreateuser', ...

And access the requested variables like this :

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

1 Comment

Thank you! That got me in the right direction. Sorry for the noob question
0

You make route of GET it must has to be post if you are posting data

node Code

app.post(..

and parameter may get

request.body.username or request.body.password

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.