0
Answered

Setting up Common sendex variables for HTTP post

Kristjan Kotkas 4 weeks ago in Tips and Tricks • updated by Ilya Markov (expert) 4 weeks ago 4

Hi


Can the definitions of the sendex http post be in a variable ? 


ie


var pre = 'Type: "POST\", Url: "/someurl"';

var body = 'this is the http post body';

var headers = ' Headers: { "content-type": "text/html;", "authorization": "Basic asdasdasdas" }'


and then this can be sent as a common string to device.sendex(pre + body + headers); 


Its really frustrating dragging the same headers for similar system calls.

GOOD, I'M SATISFIED
Satisfaction mark by Kristjan Kotkas 4 weeks ago
Under review

Hello,

yes, you can do something like this

var MyType = "POST";
var MyUrl = "/html/login";
var MyData = "Password=2007&name=authform&Login=admin";
var MyHeader = {"Content-Type": "application/x-www-form-urlencoded"};

IR.GetDevice("Server REST").SendEx({ 
      Type: MyType,       
      Url:  MyUrl,
      Data: [MyData],
      Headers: MyHeader,       
      cbReceiveText:       function(text, code, headers)    {IR.Log("cbReceiveText "+text+code+headers);},
      cbReceiveData:       function(text, code, headers)    {IR.Log("cbReceiveData "+text+code+headers);},
      cbReceiveCode:       function(code)                   {IR.Log("cbReceiveCode "+code);},
      cbReceiveKey:        function(key,value)              {IR.Log("cbReceiveKey "+key+value);},
      cbReceiveStartBody:  function(stream)                 {IR.Log("cbReceiveStartBody "+stream);},
      cbReceivePartBody:   function(stream)                 {IR.Log("cbReceivePartBody "+stream);},
      cbReceiveEndBody:    function(size)                   {IR.Log("cbReceiveEndBody "+size);}, 
      cbTimeOut:           function()                       {IR.Log("cbTimeOut");},
      cbReceiveStream:     function(stream, code, headers)  {IR.Log("cbReceiveStream "+stream+code+headers);}   
   });
   
   

Ilya, would you consider the exaple above good code ? Say you need to send an xml through post for different control items, lets say for and example remote control interface. In this case then you would have for every key on the remote the dragging headers. Examplify even more:



Lets say Body would be for 

button 1:   Data: [<xml><some><system><tag>1</tag></system></some></xml>]

button 2:   Data: [<xml><some><system><tag>2</tag></system></some></xml>]

...

button 0:   Data: [<xml><some><system><tag>3</tag></system></some></xml>]


While setting up a function for creating this data as string, the rest of the sendex should be a string too  - can it be a string is more a quiestion



So in this case the beautifyul code would be:


IR.GetDevice("Server REST").SendEx(preambula + data + headers + footer); instead of 15 lines of same repeating code.


I just hate that I would need to repeat same for each js function.


In essence I can formulate the string for the sendex


var sendexstring = ' "{" + Preambula + data + footer + "}"'

and call it IR.GetDevice("Server REST").SendEx(sendexstring);


the proble is more like that the emulator crashes if I do so..



+1

i can't understand where is the complexity. For example, you has 3 item in GUI. 

When user press first item, then a driver should send "<xml><some><system><tag>1</tag></system></some></xml>"
When user press second item, then a driver should send "<xml><some><system><tag>2</tag></system></some></xml>"
When user press third item, then a driver should send "<xml><some><system><tag>3</tag></system></some></xml>"

And Url, type and headers should be the same. Ok, you create a function with input parameter "data"

function CustomSend(data)
{
var MyType = "POST";
var MyUrl = "/html/login";
var MyData = data;
var MyHeader = {"Content-Type": "application/x-www-form-urlencoded"};

IR.GetDevice("Server REST").SendEx({ 
      Type: MyType,       
      Url:  MyUrl,
      Data: [MyData],
      Headers: MyHeader,       
      cbReceiveText:       function(text, code, headers)    {IR.Log("cbReceiveText "+text+code+headers);},
      cbReceiveData:       function(text, code, headers)    {IR.Log("cbReceiveData "+text+code+headers);},
      cbReceiveCode:       function(code)                   {IR.Log("cbReceiveCode "+code);},
      cbReceiveKey:        function(key,value)              {IR.Log("cbReceiveKey "+key+value);},
      cbReceiveStartBody:  function(stream)                 {IR.Log("cbReceiveStartBody "+stream);},
      cbReceivePartBody:   function(stream)                 {IR.Log("cbReceivePartBody "+stream);},
      cbReceiveEndBody:    function(size)                   {IR.Log("cbReceiveEndBody "+size);}, 
      cbTimeOut:           function()                       {IR.Log("cbTimeOut");},
      cbReceiveStream:     function(stream, code, headers)  {IR.Log("cbReceiveStream "+stream+code+headers);}   
   });
}

Then you should create listeners for pressing on item
IR.AddListener(IR.EVENT_ITEM_PRESS, IR.GetItem("Page 1").GetItem("Item 1"), function()
{
CustomSend("<xml><some><system><tag>1</tag></system></some></xml>")
});

and this listener you should create for each GUI item