Re: Sending Custom Headers from Client to Server

2,316 views
Skip to first unread message

greelgorke

unread,
May 16, 2013, 8:48:35 AM5/16/13
to nod...@googlegroups.com
i trieyd out, it works for me:

    console.log("emp_value:"+request.headers['custom_header']);  //emp_value:head_val

Am Donnerstag, 16. Mai 2013 12:33:19 UTC+2 schrieb Rams B:
Hi All,

I am trying sending some Custom Headers from Client to Server. The following ajax call is capable to send header information to server.

Client AJAX Call:
$.ajax({
      type: "GET",
      url: "ws_url",
      dataType: "jsonp",
       headers:{
            "custom_header":"head_val"
      },
      success: function(data) {
          alert(data)
      }
});

With above ajax call, I can get the header information at server end using JAVA based Apache CXF Web Services like below:
String client_custom_header = HttpHeaders.getRequestHeaders().getFirst("custome_header");
//I am getting the header value "head_val".

If I am trying to implement same thing in node.js with express frame work; I cannot read the headers info.

Please find my node.js code:

var https = require('http');
var express = require('express')
var app = express.createServer();

var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
};

app.configure(function () {
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
 // app.use(allowCrossDomain);
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.get('/getData', function(request, response){
 
    console.log('STATUS: ' + response.getHeader('custom_header'));//undefined

    console.log('HEADERS: ' + JSON.stringify(response.headers));//undefined
     
    console.log("emp_value:"+request.headers['custom_header']);  //undefined

    console.log("\n\n*****111*******"+request.body); //undefined
     
    console.log('params: ' + JSON.stringify(request.params));//[]

    console.log('header: ' +  JSON.stringify(request.header("Access-Control-Allow-Origin", "*")));//"*"

    console.log('body: ' + JSON.stringify(request.body));//undefined

    console.log('query: ' + JSON.stringify(request.query));//{"d":"20"}

    console.log('query: ' + request.query.d);//20
   
});
app.listen(8889);

---------------------------------------------------------

I tried several options but I couldn't. Please help me soon.........

Thanks in advance..

Ramesh

mscdex

unread,
May 16, 2013, 11:19:58 AM5/16/13
to nodejs
On May 16, 6:33 am, Rams B <bramesh.mc...@gmail.com> wrote:
>     console.log('HEADERS: ' + JSON.stringify(response.headers));//undefined

You probably want `request.headers` not `response.headers` here.

Perhaps with this change, this particular output may be helpful in
seeing what's wrong.

Rams B

unread,
May 17, 2013, 1:19:10 AM5/17/13
to nod...@googlegroups.com
Hi

Thanks for your reply.

I updated my express version to 3.2.4. and my nodejs version is 0.8.11.
I tried now, even I couldn't.

May I know which version of nodejs and express you are using.

Can you share the example codes which you tried.

Ramesh

greelgorke

unread,
May 17, 2013, 2:21:15 AM5/17/13
to nod...@googlegroups.com
Node 0.10.5 (but should work with 0.8.X too) express 3
i took your code

Srividhya

unread,
May 17, 2013, 5:27:14 AM5/17/13
to nod...@googlegroups.com
Hi,

Whether you submitted request through ajax call or using curl command.

Ultimately we want from AJAX call......

Ramesh

greelgorke

unread,
May 17, 2013, 2:28:26 PM5/17/13
to nod...@googlegroups.com
ajax calls are simple http requests, the only difference between ajax and non-ajax is how the browser react on response.

José F. Romaniello

unread,
May 17, 2013, 2:38:20 PM5/17/13
to nod...@googlegroups.com
in Express 3 you can do it as:

req.header("custom-header")
req.get("custom-header")

and also since express request is a node IncomingMessage you can use:

req.headers["custom-header"]

Please check the "subtle" differences, 

-   all three examples uses "REQUEST" not response as in one of your examples.
-   the first two examples are functions but the last on is an object

hope this help.


2013/5/17 greelgorke <greel...@gmail.com>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages