Integrate Rdstation with Django + Python p/ lead submission

Asked

Viewed 668 times

0

Good morning guys, I have a problem:

I have an LP that has to save the lead in the Postgres database and automatically send that lead to the RD platform using their API.

using the curl I do it this way:

curl -v \
      -X POST \
      -H "Content-type: application/json" \
      -d '{ "email": "[email protected]",
            "name": "raphael melo de lima", 
            "identificador": "form-landing-posp", 
            "token_rdstation": "meu_token_do_rd" 
          }' \
      'https://www.rdstation.com.br/api/1.3/conversions'

So I can send the respective lead, but I’m not able to understand how I do it using my postgres registration method.

Follows my py views.:

from django.shortcuts import render, redirect
from .models import *
from .forms import *
from django.http import JsonResponse
import json

def create_lead(request):       
    form = LeadForm(request.POST or None)

    if form.is_valid():
        data = form.cleaned_data
        lead = Lead.objects.filter(email=data['email']).first()

        if lead:
            request.session['lead_id'] = lead.id
            return redirect('registrations:videos_list')

        lead = form.save()
        request.session['lead_id'] = lead.id
        return redirect('registrations:videos_list')

    return render(request, 'index.html', {'form':form})

def videos_list(request):

    if 'lead_id' in request.session:
        lead = Lead.objects.get(pk=request.session['lead_id'])


        return render(request, 'videos-list.html')

    return redirect('registrations:create_lead')

Can anyone give me that strength?.

1 answer

2


Using requests:

import requests

requests.post(
    'https://www.rdstation.com.br/api/1.3/conversions',
    json={
        'email': '[email protected]',
        'name': 'raphael melo de lima',
        'identificador': 'form-landing-posp',
        'token_rdstatuion': "meu_token_do_rd",
    }
)

So just put this in your code, with the variables you want to send.... data['email'] and data['name'] for example?

  • is well anyway, thank you very much for the help guy I managed to do the POST, and I made an improvement that I found pertinent url ="https://www.rdstation.com.br/api/1.3/conversions"
 headers = {'Content-type': 'application/json'}
 data = {'name':lead.name,
 'email':lead.email,
 'identificador': 'form-landing-posp',
 'token_rdstation': 'meu_token'}
 r = requests.post(url, json=data, headers=headers)
 r.json()

  • 1

    @Raphaelmelodelima Spend headers is useless and reductive, when using the parameter json= in the requests.post() the content-type is automatically set to application/json without having to specify separately, so you should remove this parameter and do as in my reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.