Using HTTP headers

  1. Introduction
  2. Sample application

Introduction

HTTP requests can use headers to describe additional properties of the request, like authentication, content type, etc. On this page, you will learn how to use them. Enjoy!

Sample application

The sample application builds the HTTP request buffer (method, headers, path and payload), converts the buffer to hexadecimal and send it to the host, through the command network.send. After that, 4 bytes are requested, related to the size of the answer, through the command network.receive, and after a validation, the complete answer is requested.

Watch out the spaces, because some variables will be concatenated!

<!-- HTTP verb -->
<stringvariable value="GET " variable="sVerb" />
<!-- Path -->
<stringvariable value=' /balance/ ' variable="sPath" />
<!-- Data to be sent -->
<stringvariable value='{"object":"balance","paymentData":{"cardNumber":"1J212eZvKYlo2CLJXYQ"}}' variable="sPayload" />
<!-- Headers, comma separated: header1:value1,header2:value2 -->
<stringvariable value="Authorization: Basic dWlvMHE2WFphZDN4R==:,Content-Type:application/json" variable="sHeaders" />

<!-- Concatenate the strings in this order: Method (sVerb), Headers (sHeaders), Path (sPath), Payload (sPayload) -->
<joinstring firstvalue="$(sVerb)" secondvalue="$(sHeaders)" variabledestination="$(sBuffer)" />
<joinstring firstvalue="$(sBuffer)" secondvalue="$(sPath)" variabledestination="$(sBuffer)" />
<joinstring firstvalue="$(sBuffer)" secondvalue="$(sPayload)" variabledestination="$(sBuffer)" />

<!-- The content of the variable buffer at the end should be:

GET Authorization: Basic dWlvMHE2WFphMUt0ZDN4R==,Content-Type:application/json /balance/ {"object":"balance","paymentData":{"cardNumber":"14sJ212eZvKYlo2CLJXYQFYd"}}

Note the spaces between the parts of the content -->

<!-- Convert the buffer to hexadecimal -->
<integervariable value="0" variable="iSize" />
<string.tohex string="$(sBuffer)" variablereturn="$(sBuffer)" />
<string.length value="$(sBuffer)" variablereturn="$(iSize)" />

<!-- Send the hexadecimal buffer to the host with the command network.send -->
<integervariable value="0" variable="iRet" />
<network.send buffer="$(sBuffer)" size="$(iSize)" variablereturn="$(iRet)" />

<!-- Receive the size of the answer (4 bytes) from the host with the command network.receive -->
<stringvariable value="" variable="sBufferReceive" />
<integervariable value="0" variable="iBytesReceive" />
<network.receive variablebuffer="$(sBufferReceive)" maxsize="4"
                 variablereceivedbytes="$(iBytesReceive)" variablereturn="$(iRet)" />

<!-- Validate if it was possible to receive the size of the answer from the host -->
<if variable="$(iRet)" operator="notequalto" value="1">
  <cleandisplay />
  <display line="1" column="3" message=" COMMUNICATION " />
  <display line="2" column="3" message="     ERROR     " />
  <waitkeytimeout seconds="5" />
  <network.hostdisconnect />
  <exit />
</if>

<!-- Receive the complete answer from the host with the command network.receive -->
<string.fromhex string="$(sBufferReceive)" variablereturn="$(sBufferReceive)" />
<convert.toint base="16" number="$(sBufferReceive)" variablereturn="$(iSize)" />
<network.receive variablebuffer="$(sBufferReceive)" maxsize="$(iSize)"
                 variablereceivedbytes="$(iBytesReceive)" variablereturn="$(iRet)" />

<!-- Validate if it was possible to receive the complete answer from the host -->
<if variable="$(iRet)" operator="notequalto" value="1">
  <cleandisplay />
  <display line="1" column="3" message=" COMMUNICATION " />
  <display line="2" column="3" message="     ERROR     " />
  <waitkeytimeout seconds="5" />
  <network.hostdisconnect />
  <exit />
</if>

<!-- Convert the hexadecimal answer and display it -->
<string.fromhex string="$(sBufferReceive)" variablereturn="$(sBufferReceive)" />
<cleandisplay />
<display line="1" column="3" message=" RECEIVED: " />
<display line="2" column="0" message="$(sBufferReceive)" />
<waitkeytimeout seconds="5" />
<network.hostdisconnect />