iamtoc iamtoc - 4 months ago 13
JSON Question

How to post form data for new person to Pipedrive (API) via REST

Here is the form that does not work:

<form action="https://api.pipedrive.com/v1/persons" method="post">

<!-- These hidden fields have valid values, just not shown here -->
<input type="hidden" name="owner_id" id="owner_id" value="my-id" />
<input type="hidden" name="org_id" id="org_id" value="my-company-id" />
<input type="hidden" name="api_token" id="api_token" value="my-api-key" />
<input type="hidden" name="stage_id" id="stage_id" value="35" />

<b>Name</b><br/>
<input type="text" name="name" id="name" id="name"/>
<br/><br/>

<b>E-mail</b><br/>
<input type="text" name="email" id="email" id="email"/>
<br/><br/>

<b>Arrival</b><br/>
<input type="text" name="2fdf1284127d702e42595ce20bd8ffdf60763105" id="2fdf1284127d702e42595ce20bd8ffdf60763105"/>
<br/><br/>

<b>Departure</b><br/>
<input type="text" name="2492a5afed2a9cb7948d6a22135fd4dd80de200c" id="2492a5afed2a9cb7948d6a22135fd4dd80de200c"/>
<br/><br/>

<b>Message</b><br/>
<textarea style="width:300px; hight:70px;" id="7433280b87ffc7c1e3fd615eb35526273bcea6cf" name="7433280b87ffc7c1e3fd615eb35526273bcea6cf"></textarea>
<br/><br/>
<input type="submit" value="Remitir"/>
</form>


Upon submission, the following response is returned:

{"success":false,"error":"Organization not found.","data":null,"additional_data":null}


What is missing for a successful addition of adding a new person via REST?

Here is the API: https://developers.pipedrive.com/v1

Many thanks for a solution.

Answer

You can do something like that

<script language="javascript" type="text/javascript">
    function pDriveIntegration()
    {
       var url = "https://api.pipedrive.com/v1";
       var rsrc_deals = "/deals";
       var api_token = "YOUR_API_TOKEN";
       var deal
Json = '{' + '"title":"' + name + '","org_id":"?"' +
                    ',"value":"0"' + ',"currency":"GBP"' + ',"stage_id":"?"' + 
',"visible_to":"?"' + ',"status":"open"' + '"}';


        var xhrDeal = new XMLHttpRequest();
        xhrDeal.onreadystatechange = function()
        {
            if (xhrDeal.readyState == 4 && xhrDeal.status == 201) {
                var obj = JSON.parse(xhrDeal.responseText);
                var deal_id = obj.data.id;
            }
        }
        xhrDeal.open("POST", url + rsrc_deals + "?" + "api_token" + "=" + api_token, false);
        xhrDeal.setRequestHeader("Content-type", "application/json");
        xhrDeal.send(dealJson);

</script>
</head>
<body>

<form ...........>


<input type="submit" onclick="pDriveIntegration();"


</form>

Therefore you need AJAX. Even if it were possible to do that only with a HTML form, you have to encode a JSON to send with the post. To be honest, I tried but I always got some errors in any browser.

I noticed that is no secure at all, because somebody is able to see your source code and get your API_TOKEN through the browser. I mean someone could get all your info (I moved to other solutions, not using only HTML & AJAX).

But anyway it is a good beginning to understand everything that you need.

I'd also suggest to think about the "persons". For example, before than create a new deal I find the person. If he/she exist I won't create it, I get its id and I am gonna put on the Deal.

But, in the other case if the person doesn't exist I create a person before than all. After that, I'll use its id on the Deal.

I hope it will be useful.

Byee

Comments