Action: HTTP request June 17, 2023 03:09 Updated Index: Orientation video What is an HTTP request? Situations in which we can use an HTTP request in Builder How to make an HTTP request in Builder? Example HTTP Request with Authentication Learn what an HTTP request is, situations in which we can use an HTTP request and how to make an HTTP request in Builder to obtain user data from their zip code or CPF, as well as an e-commerce request from the request code. Orientation video: What is an HTTP request? When a website is accessed, the browser (Google, Edge, Opera, etc.) sends an HTTP request to the server , requesting specific information . The server looks for what was requested in its files, and when it finds it, it sends what was found as a response that is interpreted by the browser , displaying it in the form of a website on a screen. Technically, HTTP is a protocol based on text streams. It is a protocol for communication and transfer of HTML pages . Briefly, the client sends requests (requests), and receives responses from the server (responses), as shown in the following figure. The HTTP protocol defines a set of request methods responsible for indicating the action to be performed for a given resource . Example methods: GET, POST, PUT, DELETE, PATCH We will use the GET and POST methods as an example to make an HTTP request within Blip and for that we need to fill in some fields indicating what we want in our request. GET method: GET requests are recommended to obtain data from a given resource. POST method: POST requests are most often used to send information to be processed , such as creating some resource, such as a product, or a customer. Situations in which we can use an HTTP request in Builder Get a user's full address from their zip code; Obtain data such as a user's name, address or affiliation from their CPF; Obtain data from an e-commerce order from the order code; Note: if the API used is public, no authentication method is required. How to make an HTTP request in Builder? Here's a step-by-step example of how to make HTTP requests through Builder . In this example, the objective is to obtain the data of a user's address from the typed zip code. For this we will use the public API of the post office. 1) The first step is to CREATE YOUR CHATBOT FLOW and IDENTIFY IN WHICH BLOCK THE USER WILL ENTER THE INFORMATION that will be fetched from the API. 2) In this block, CLICK ON THE MESSAGE IN WHICH THE USER WILL ENTER THE INFORMATION THAT WILL BE STORED and later searched in the API. 3) After clicking on the message, click on “SAVE ANSWER IN VARIABLE” and type a name of interest in the “VARIABLE” field (in this case the zip code). Thus, the information entered by the user will be saved and he will be able to use this variable later. 4) Then, access the “ACTIONS” option in the top menu of this same block. 5) From there, slide the bar and in “OUTPUT ACTIONS” click on: “ADD OUTPUT CONDITIONS” , select: “HTTP REQUEST” . 6) After selecting the type of method (GET or POST) you must ENTER THE URL OF THE API where the request will be requested and the NAME OF THE VARIABLE stored (created in step 2) ENTER KEYS to carry out the search according to what the user typed . 7) After that, in the block where we will have the bot’s response according to the HTTP request response, click on “ACTIONS” → ADD INPUT ACTION → EXECUTE SCRIPT 8) In the script part, create a function that will receive the input variable . Inside the function, create a variable that will receive “JSON.parse(input)” and then return the desired property. Save the response variable in the “ ENTRY VARIABLES” field , as in the example below with: “corpocep” , so that it can be returned in the chat. Input variables are received as parameters in the JavaScript function. 9) The variables created are used between braces in the bot block and are replaced by the values found after the request. 10) Done! Now, in the user's view, when using the chatbot, the messages are displayed as shown below: 11) If you need to change something in the script, click on “ RUN SCRIPT ” and access the saved script. You can now test your chatbot! Example HTTP Request with Authentication Next we will exemplify how to make an HTTP request containing the BLIP authentication . In this example, we want to get the emails of all users who have given their email to the chatbot. For example, let's assume that 10 people used the chatbot and provided their own email. Through this HTTP request, we get the 10 emails typed by these people. This request can also be used to obtain the telephone number, CPF, name and other data entered by the user. Get contacts: The first step is to create a block where the desired information will be requested . In our example, we ask for the user's email in the “Boas Vindas” block. In the user input text, click on the text and enable the option: “SAVE ANSWER IN VARIABLE” , then type a name for this variable . In this way, you are storing the answer typed by the user, in order not to lose this data. Create a new block , in this block the HTTP request will be executed. In our example, this block is the “Searching Contacts” block. 4) Access the “ACTIONS” tab , click on “ADD ENTERING ACTION" and select “HTTP REQUEST” . 5) Access the request to be used in BLIP Docs and check if the method is POST or GET . So, fill in the method type in the 1st field of the Builder . Then, copy the URL next to the method type , in our example the URL is: https://{{contract_id}}.http.msging.net/commands and paste it in the 2nd field of the Builder, following the images below 6) Then click on the settings icon on the top right side of the screen: 7) Access “CONNECTION INFORMATION” in the left side menu: 8) Scroll to the bottom and find “HTTP ENDPOINTS”, copy the first field “AUTHENTICATION HEADER (AUTHORIZATION)”. 9) Return to the block where you created the HTTP Request Action, in the ACTIONS tab → “HTTP REQUEST” , access “HEADERS” and type “AUTHORIZATION” in the “KEY” field and paste the previously copied text in the “VALUE” field . 10) Return to BLIP Docs and copy the “ BODY ” from the request : {"id": "{{$guid}}","to": "postmaster@crm.msging.net","method": "get","uri": "/contacts?$skip=0&$take=3"} 11) Within the HTTP Request, access “BODY” and paste the copied code. 12) Below “BODY” , go to “SAVE ANSWER” and fill in the fields with the name you prefer to store the answers obtained . 13) Create a new Block , where the request return will be displayed. Create a text with the “ RETURN ” variable created in the previous step. Make sure that the variable name is the same as what you created and that the variable is enclosed in two curly braces. 14) Click on the ROCKET ICON to publish the stream and then on the test bot icon: 15) When testing the bot, as soon as the user enters the email, the variable {{return}} will return all emails from the contacts, that is, in this way it is possible to obtain the emails typed by users in the chatbot through an HTTP request ! For more information, visit the discussion on the subject in our community or the videos on our channel. 😃 Related articles Builder variables Sending WhatsApp Active Messages on Blip Desk Registration of Tags by Line Service History How to connect to a personalized service channel