0

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

Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
RMDev
  • 3
  • 2

1 Answers1

0

[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.

Shreyans
  • 1,738
  • 1
  • 13
  • 19
  • The serverside works with httpie and postman as expected, but has a problem when the request comes from gupshup. any thoughts? – RMDev Aug 18 '17 at 10:49
  • is it possible for you to share the details of the API or the Postman curl command for me to test? If not here then you can write to us on developer@gupshup.io – Shreyans Aug 18 '17 at 10:58