Search code examples
jsondjangopython-3.xdjango-rest-frameworkgupshup

Gupshup Post - Empty Body


I'm building a simple bot that makes an http post call passing in JSON objects. The server responds back with the error - {"detail":"JSON parse error - Expecting value: line 1 column 1 (char 0)"}:

I don't think the server side is the issue; I've tried the request using httpie.

The code in Gupshup

var contextParam = {
    "botname": event.botname, 
    "channel": event.channel, 
    "sender": event.sender,
    "message":event.message
};
var url = "https://abcserver.com/sm/postData";
var param = JSON.stringify(contextParam);
var header = {"Content-Type": "application/json"};
context.simplehttp.makePost(url, param, header) 

The corresponding call from httpie

http POST https://abcserver.com/sm/postData  botname=MrBot channel=Skype sender=MrSender message=Hi

At the server side:

logger.debug("Request body : " + str(request.body))

puts - "Request body : b'" in the log file.

PS: I'm using Django, Django Rest Framework


Solution

  • [Answer Update 21/8/2017]

    The syntax for making the HTTP POST call using the IDE Bot Builder of Gupshup is correct. Check out this post making-http-post-request-on-gupshup-ide-works on SO where a working code is present.

    Complete working code:

     var url_ = 'http://p-curl-i.herokuapp.com/getresponse/';
    var body = {
        "botname": event.botname,
        "channel": event.channel,
        "sender": event.sender,
        "message": event.message
    };
      var headers = {
    'cache-control': 'no-cache',
        'content-type': 'application/json',
        'Content-Length' : JSON.stringify(body).length 
    };
       context.simplehttp.makePost(url_, JSON.stringify(body), headers);
    

    You need to send "Content-Length" because your server has this as mandatory and Gupshup's backend is not sending the content length by default as Postman or httpie does.