Skip to content

Instantly share code, notes, and snippets.

@leadbi
Created April 1, 2021 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leadbi/2b8aa8fad7936971a21242dc446a192a to your computer and use it in GitHub Desktop.
Save leadbi/2b8aa8fad7936971a21242dc446a192a to your computer and use it in GitHub Desktop.
LeadBI Java Example
import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Payload to be sent to the api
*/
class LeadBIContact implements Serializable {
public String email;
public String first_name;
public String last_name;
public String prospect_id;
public String city;
public String country;
public Boolean privacy_consent;
}
public class JavaAPIExample {
/**
* The main function
* @param args
*/
public static void main(String[] args) {
System.out.println("Start creating contact");
// Create a new contract instance
var contact = new LeadBIContact() {{
email = "hfhmzwbeb@supere.ml";
first_name = "Pallino";
last_name = "Bullet";
prospect_id = "unknown";
city = "Milan";
country = "IT";
privacy_consent = true;
}};
try {
// Call the api endpoint
CreateContact(contact);
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Contact created");
}
/**
* Create a new contact if not exists
* @param contact
* @throws JsonProcessingException
* @throws IOException
* @throws InterruptedException
*/
public static void CreateContact(LeadBIContact contact)
throws JsonProcessingException, IOException, InterruptedException {
// Define the api endpoint
var apiEndpoint = "https://app.leadbi.com/api/v1/visitors/468/all/create_contact";
// Create the json payload
ObjectMapper objectMapper = new ObjectMapper();
var requestBody = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(contact);
// Print the payload
System.out.println(requestBody);
// Create a request
var request = HttpRequest.newBuilder(URI.create(apiEndpoint)).header("Content-Type", "application/json")
.header("Accept", "application/json")
.header("x-access-id", "your id")
.header("x-access-secret", "your secret")
.POST(BodyPublishers.ofString(requestBody)).build();
// Create client
var client = HttpClient.newHttpClient();
// Send request
var response = client.send(request, BodyHandlers.ofString());
// Print response
System.out.println(response.body());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment