Page tree

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »


В данной статье рассматриваются способы отправки API запросов через RabbitMQ.

Для идентификации запросов посылается уникальный параметр request_id.


Во время работы с АPI в RabbitMQ используются две очереди:

  1. api_req - очередь для входящих запросов;
  2. api_resp - очередь, в которую попадают ответы на запросы (только если указан request_id)

Structure of the sent JSON message






request_idstring"abcd1234"Yes, if the response to to request is necessaryRequest ID

YesAPI request path
bodyJSON object

"body": {
            "_fname": "Fname1",
            "_lname": "Lname1",
            "email": ""
    "email": "",
    "db_id": 1,
    "detect_geo": true,
    "token": "abcdefghijklmnopqrstuvwxyzABCD"

YesAPI request body

Structure of the received JSON message





bodyJSON object

        "error_text":"Successful operation",

Request ID
request_idstring"abcd1234"Идентификатор запроса

How to send API requests in Go

You can send an API request using a script that calls RabbitMQ:

  • Write and execute a script

package main

import (


const accID = 1
const resourceID = 3
const dbID = 23
const msgID = 17
const segmentID = 85
const amountOfPushMsgs = 1
const emailDomain = ""

const req = `{
	"account_id": 1,
	"request_id": "db1894e4-1a2c-4021-8233-9cca0b96b79e",
	"request": "api/v1.1/campaigns/triggers/import_and_start_batch",
    "body": {
		"token": "abcdefghijklmnopqrstuvwxyzABC"
		"format": "json",
		"trigger_id": 240,
		"skip_triggers": false,
		"detect_geo": true,
		"matching": "custom",

		"data": [
					"email": "",
					"email": "",

func failOnError(err error, msg string) {
	if err != nil {
		log.Fatalf("%s: %s", msg, err)

func main() {
	var err error

	var conn *amqp.Connection
	conn, err = amqp.Dial("amqp://example:abcdefghijklmnopqrstuvwxyz127.0.0.1:5672/")
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	var ch *amqp.Channel
	ch, err = conn.Channel()
	failOnError(err, "Failed to open RabbitMQ channel")
	defer ch.Close()

	qUeueImport, err := ch.QueueDeclare(
		"api_req", // name
		true,      // durable
		false,     // delete when unused
		false,     // exclusive
		false,     // no-wait
		nil,       // arguments
	failOnError(err, "Failed to declare RabbitMQ queue")

	for i := 0; i < amountOfPushMsgs; i++ {
		var bodyMap = make(map[string]interface{})
		err = json.Unmarshal([]byte(req), &bodyMap)
		failOnError(err, "Failed json.Unmarshal to bodyMap")

		for k, v := range bodyMap {
			log.Println(k, v)

		body, err := json.Marshal(bodyMap)
		failOnError(err, "Failed to json.Marshal bodyMap")

		err = ch.Publish(
			"",               // exchange
			qUeueImport.Name, // routing key
			false,            // mandatory
			false,            // immediate
				ContentType: "text/plain",
				Body:        []byte(body),
		failOnError(err, "Failed to publish a message")

	log.Printf("[v] Sended %d requests to %s", amountOfPushMsgs, qUeueImport.Name)
  • After executing the script, go to RabbitMQ → Queues

  • Choose api_resp  and get a response to the API request

How to send API requests via RabbitMQ Management Plugin

You can send an API request directly to RabbitMQ via api_req:

  • Go to RabbitMQ → the Queues tab. Then select api_req and Publish message.

  • After the request is sent, you can see its result in the same way as in the first method.
  • No labels