0

i should be send data on /node_api (python+djnago), i use node.js with socket.io:

socket.on('send_message', function (message) {
        values = querystring.stringify({
            comment: message, // not null
            sessionid: socket.handshake.cookie['sessionid'],
        });

        var options = {
            host: 'localhost',
            port: 8000,
            path: '/node_api',
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': values.length
            }
        };

        var req = http.get(options, function(res){
            res.setEncoding('utf8');

            res.on('data', function(message){
                if(message != 'work'){
                    console.log('Message: ' + message);
                }
            });
        });

        req.write(values);
        req.end();

but data does not sended, in my views.py:

request.POST.get('comment','null') # null

request is ok, but why POST is empty?

if i console.log(req) i look

 method: 'GET',
  path: '/node_api',
  _events: { response: [Function] },
  _headers: 
   { 'content-type': 'application/x-www-form-urlencoded',
     'content-length': 52,
     host: 'localhost:8000' },
  _headerNames: 
   { 'content-type': 'Content-Type',
     'content-length': 'Content-Length',
     host: 'Host' },
  _header: 'GET /node_api HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 52\r\nHost: localhost:8000\r\nConnection: keep-alive\r\n\r\n',

i dont know, but method is GET...and GET null too

please help me!!!

Queen johniek
  • 161
  • 1
  • 1
  • 7
  • I think this one can help you: http://stackoverflow.com/questions/4762086/socket-io-client-library-in-python – udidu Feb 04 '13 at 17:12

1 Answers1

0

Did you checked Error code? It may be a CSRF checking problem.

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_api_view(request):
    return HttpResponse("text")

Can you try this?

christophe31
  • 6,359
  • 4
  • 34
  • 46