Make a POST request to send JSON data to an external API endpoint using Deluge.
When a deal is won, you might need to push that data to an external ERP system, a custom database, or a Slack webhook. This requires constructing a JSON payload and executing an HTTP POST request.
While GET requests accept a Deluge Map for parameters, POST requests to modern JSON APIs require a raw stringified JSON body.
Notice in the script that we build our payload as a standard Deluge Map(). However, when we pass it to the invokeurl task, we use payload.toString().
This is the most critical step. If you pass the map directly, Deluge defaults to sending the data as application/x-www-form-urlencoded, which will cause 400 Bad Request errors on most modern JSON REST APIs.
By explicitly converting the map to a string, and explicitly declaring the Content-Type header as application/json, you guarantee the receiving server parses your payload perfectly.
headerMap = Map();
headerMap.put("Authorization", "Bearer YOUR_TOKEN");
headerMap.put("Content-Type", "application/json");
payload = Map();
payload.put("first_name", "Sunny");
payload.put("email", "sunny@example.com");
response = invokeurl
[
url: "https://api.example.com/v1/users"
type: POST
parameters: payload.toString()
headers: headerMap
];
info response;