4

Bitrix24 CRM have webhook functional to add leads (clients) to CRM. All documentation is written on php, but I want to use ASP.NET. Here's how they do it on php:

$queryUrl  = 'https://restapi.bitrix24.ru/rest/1/31uhq2q855fk1foj/crm.lead.add.json';
$queryData = http_build_query(array(
    'fields' => array(
        "TITLE" => "NEW LEAD"
    ),
    'params' => array(
        "REGISTER_SONET_EVENT" => "Y"
    )
));

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_SSL_VERIFYPEER => 0,
    CURLOPT_POST => 1,
    CURLOPT_HEADER => 0,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $queryUrl,
    CURLOPT_POSTFIELDS => $queryData
));

$result = curl_exec($curl);
curl_close($curl);

$result = json_decode($result, 1);

I'm trying to do the same thing using ASP.NET, but get error 400 as a response. I almost sure that the problem is in the request parameters, line const string data = @"[{""fields"":{""title"":""Test""}}]";. I've tried tons of combinations, but nothing worked.

const string url = @ "https://companyname.bitrix24.ru/rest/14/31uhq2q855fk1foj/crm.lead.add.json";
const string data = @"[{""fields"":{""title"":""Test""}}]";

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
byte[] cred = Encoding.UTF8.GetBytes("email:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

HttpContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage messge = client.PostAsync(url, content).Result;
string description;
if (messge.IsSuccessStatusCode) {
    string result = messge.Content.ReadAsStringAsync().Result;
    description = result;
}
randomsuffer
  • 353
  • 2
  • 7
  • 15

3 Answers3

6

Bitrix24 support are very "happy" with php and does not know about other languages :( After some investigations I found way with anonymous objects and json.net. Your sample should looks like:

var data = new {
  fields = new {
    TITLE = "NEW LEAD"
  },
  @params = new {
    REGISTER_SONET_EVENT = "Y"
  }
};
var contentText = JsonConvert.SerializeObject(data);

var content = new StringContent(contentText, Encoding.UTF8, "application/json");
// and so on with HttpClient

Update Dec 13:

Sometimes you can't (or don't want) put field names directly into anonymous object. So, dictionary may be used:

var data = new
{
    ID = someId,
    FIELDS = new Dictionary<string, object>()
    {
        [options.SomeFieldName] = fieldValue,
    },
};
Dmitry
  • 16,110
  • 4
  • 61
  • 73
1

I solved the problem with this code:

public void CreateLead( string title, decimal opportunity, string contactName, string phoneNumber, string email )
    {
        try
        {                
            string accessToken = GetNewAccessToken();

            string url = string.Format( "https://{0}/rest/crm.lead.add.json", portal_name );

            var data = new
            {
                fields = new
                {
                    TITLE = title,
                    CURRENCY_ID = "RUB",
                    STATUS_ID = "NEW",
                    OPENED = "Y",
                    OPPORTUNITY = opportunity,
                    ASSIGNED_BY_ID = 46,
                    COMPANY_TITLE = contactName,
                    PHONE =  new List<PHONE>() { new PHONE() { VALUE_TYPE = "WORK", TYPE_ID = "PHONE", VALUE = phoneNumber } }.ToArray(),
                    EMAIL = new List<EMAIL>() { new EMAIL() { VALUE_TYPE = "WORK", TYPE_ID = "EMAIL", VALUE = email } }.ToArray()
                },
                @params = new
                {
                    REGISTER_SONET_EVENT = "Y"
                }
            };

            BitrixLead lead = new BitrixLead();

            lead.TITLE = title;
            lead.CURRENCY_ID = "RUB";
            lead.STATUS_ID = "NEW";
            lead.OPENED = "Y";
            lead.OPPORTUNITY = opportunity.ToString();                

            if (!string.IsNullOrEmpty( contactName ))
                lead.COMPANY_TITLE = contactName;

            if (!string.IsNullOrEmpty( phoneNumber ))
                lead.PHONE = new List<PHONE>() { new PHONE() { VALUE_TYPE="WORK", TYPE_ID="PHONE", VALUE = phoneNumber }}.ToArray();

            if (!string.IsNullOrEmpty( email ))
                lead.EMAIL = new List<EMAIL>() { new EMAIL() { VALUE_TYPE = "WORK", TYPE_ID = "EMAIL", VALUE = email } }.ToArray();


            PostToAPI( url, accessToken, data );
        }
        catch (Exception exc)
        {
        }
    }

private void PostToAPI( string url, string token, object data )
    {
        string json = Newtonsoft.Json.JsonConvert.SerializeObject( data );

        var http = (HttpWebRequest)WebRequest.Create( new Uri( url ) );
        http.Accept = "application/json; charset=utf-8";
        http.ContentType = "application/json; charset=utf-8";
        http.Method = "POST";            
        http.Headers.Add( "Authorization", "Bearer " + token );

        UTF8Encoding encoding = new UTF8Encoding();
        Byte[] bytes = encoding.GetBytes( json );

        Stream newStream = http.GetRequestStream();
        newStream.Write( bytes, 0, bytes.Length );
        newStream.Close();

        var response = http.GetResponse();            
    }
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.[Read this](https://stackoverflow.com/help/how-to-answer). – Shanteshwar Inde Jan 30 '19 at 07:03
-1
Public Sub webServicesDatos(params As String, ws As String, idusuario As Integer, accion As String, catalogo As String,
                                Idcampo As String, registro As Integer, idregistro As Integer, pagina As String,
                                 cat As String, nomcampo As String, bd As String, campoUpdate As String, SistemasNom As String, IdTransaccion As Integer,
                                Proceso As Integer, Evento As Integer, Usuarioadd As String)
        qryLogBitrix = ""
        request = WebRequest.Create(ws & params)
        postData = params
        Dim data As Byte() = Encoding.UTF8.GetBytes(postData)

        request.Method = "POST"
        request.ContentType = "application/json"
        request.ContentLength = data.Length

        Dim stream As Stream = request.GetRequestStream()
        stream.Write(data, 0, data.Length)
        stream.Close()

        response = request.GetResponse()
        Using reader As New StreamReader(response.GetResponseStream())
            SR2 = reader.ReadToEnd()
        End Using

        Dim ValidaResult As Object = New JavaScriptSerializer().Deserialize(Of Object)(SR2)
End Sub


Dim consumir As ConsumirRestApi = New ConsumirRestApi
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim builder As StringBuilder = New StringBuilder()
    If nombre <> "" Or ap_paterno <> "" Or ap_materno <> "" Then
        parametros = HttpUtility.UrlEncode("FIELDS[TITLE]") + "=" + HttpUtility.UrlEncode(nombre) + " " + HttpUtility.UrlEncode(ap_paterno) + " " + HttpUtility.UrlEncode(ap_materno)
        builder.Append(parametros)
    End If
    If nombre <> "" Or nombre <> Nothing Then
        parametros = "&" + HttpUtility.UrlEncode("FIELDS[NAME]") + "=" + HttpUtility.UrlEncode(nombre)
        builder.Append(parametros)
    End If
    If idExpediente <> "0" Or idExpediente <> Nothing Or idExpediente <> "" Then
        parametros = "&" + HttpUtility.UrlEncode("FIELDS[UF_ID_EXPEDIENTE]") + "=" + HttpUtility.UrlEncode(idExpediente)
        builder.Append(parametros)                                     
    End If

    If accion = "INSERT" Then
        carpeta = "crm.lead.add.json?"                                  
    ElseIf accion = "UPDATE" Then
        carpeta = "crm.lead.update.json?"
    ElseIf accion = "DELETE" Then
        carpeta = "crm.lead.delete.json?"
    End If

    consumir.webServicesDatos(builder.ToString, "https://" & ip & "" & carpeta & "", idUsuario, accion, procesos, Id_formulario, id_bitrix, idCiclo, ruta, cat, nomcamp, bdinter, nomcampo, sistemanom, idCiclo, pro, evento, "")

End Sub
Kevin
  • 16,549
  • 8
  • 60
  • 74