Requests not working

I’m developing a custom app and I’m having the following problem…

I’m getting custom fields from a ticket as follows:

onTicketCreateHandler: function (args) {

var requestTicketCustomFields = args.data.ticket.custom_fields;

var client = args.data.ticket.custom_fields.cf_cliente_1586027;

var produtooperadora;

var categoria;

var subcategoria;

for (var property in requestTicketCustomFields) {
  if (requestTicketCustomFields[property] != null) {
    if (property.includes("cf_produtooperadora")) {
      produtooperadora = requestTicketCustomFields[property]
    }
    if (property.includes("cf_categoria")) {
      categoria = requestTicketCustomFields[property]
    }
    if (property.includes("cf_subcategoria")) {
      subcategoria = requestTicketCustomFields[property]
    }
  }
}

The code above is working normally.

After executing this part I create an object to receive the information I want, like this:

const apiRequest = {

      Category_: categoria,

      SubCategory_: subcategoria,

      ProductOperator_: produtooperadora,

      Client_: client

    }

Once that’s done, I send this object to a third party API where I’ll check the information of this object and return some information. I’m sending this object as follows:

var response = {

      headers: {

        "Content-Type": "application/json"

      },

      body: JSON.stringify(apiRequest)

    }

    var sla;

    $request.post("https://url.net/api/RelationshipRecord", response)

      .then(

        function (data) {

          sla = data;

        },

        function (error) {

          console.log("O erro abaixo ocorreu ao efetuar a requisição que retorna o sla.");

          console.error(error);

        }

      );

So far, everything is working. The next step would be to post the variable that received the API data. However, this is where the error arises, when I try to post the variable on the webhook.site site it returns a “No content”, I did some tests on my API using postman and it returns the information normally. I don’t know why this is happening… the way I’m trying to send this variable is as follows:

var responseApiCsharp = {

        headers: {

          "Content-Type": "application/json"

        },

        body: JSON.stringify(sla)

      }

      $request.post("https://webhook.site", responseApiCsharp)

        .then(

          function (data) {

            console.log(data);

          },

          function (error) {

            console.error(error);

          }

        );
1 Like

Hi @Lucas_de_Lagos_Pando

Where are you calling the post to “https://webhook.site”?
It should after the attribution to “sla” var.
Remember that you are dealing with async calls.

2 Likes

Hi @samuelpares

I redid the last bit of code a little bit differently… I didn’t understand why it wasn’t working before, but I’ll leave the code up to date here:

  $request.post("https://url/api/RelationshipRecord", response)
    .then(
      function (data) {
        obj = JSON.parse(data.response);

        var responseApiCsharp = {
          headers: {
            "Content-Type": "application/json"
          },
          body: JSON.stringify(obj)
        }

        $request.post("https://webhook.site", responseApiCsharp)
          .then(
            function (data) {
              console.log(data);
            },
            function (error) {
              console.error(error);
            }
          );
      }

The change I made was the sla variable I replaced with “obj = JSON.parse(data.response);” and made the post to the webhook.site in the promisse itself.

1 Like

Is it working now?
This code looks fine.

1 Like

@samuelpares

Yes, now the code is working normally.

Thanks

3 Likes