Utilizando headers HTTP

  1. Introdução
  2. Aplicação de exemplo

Introdução

Requisições HTTP podem utilizar cabeçalhos (headers) para descrever propriedades da requisição, como autenticação, tipo de conteúdo, etc. Nesta página, você aprenderá como usá-los. Divirta-se!

Aplicação de exemplo

A aplicação de exemplo monta o buffer com a requisição HTTP (método, cabeçalhos, caminho e payload), converte o buffer para hexadecimal e o envia para o host, através do comando network.send. Em seguida, são requisitados 4 bytes, referente ao tamanho da resposta, através do comando network.receive, e após a validação, a resposta completa é requisitada.

Atente-se aos espaços, pois algumas variáveis serão concatenadas!

<!-- 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 />