Hello Zoho,
When I am posting the request to your server from the API (Records APIs -- > Single Records APIs -- > Insert specific record)
to insert the Leads I am getting
Bad Request error. I don’t understand why this happened. Previously I am posting the request for the Access Token and its response is correct but when I called the APIs to insert the Leads using generated Access Token I am getting the Bad Request Error.
Here is my code in c# to post the request.
-
To Get Access Token
var AuthenticationUrl = Access_Token_Url + Refresh_Token + "&client_id=" + Client_Id + "&client_secret=" + Client_Secret + "&grant_type=" + Grant_Type;
ServicePointManager.Expect100Continue = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(AuthenticationUrl);
request.Method = "POST";
request.ContentType = "application/json";
WebResponse response = request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
json = sr.ReadToEnd();
}
var serializer = new JavaScriptSerializer();
var AccessToken = serializer.Deserialize<ZohoCRMLeadsFields>(json);
access_Token = AccessToken.Access_token.ToString();
This code is working perfectly to get Access Token.
-
To Insert specific Leads
private static bool SaveLeads(string access_Token, ZohoCRMLeadsFields zohoCRMLeadsFields)
{
try
{
HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMilliseconds(600000);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.BaseAddress = new Uri(ZohoAPI_Base_URL);
var HeadWithAuthenticationKey = "Zoho-oauthtoken " + Access_Token.Trim();
httpClient.DefaultRequestHeaders.Add(HttpRequestHeader.Authorization.ToString(), HeadWithAuthenticationKey);
httpClient.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), "application/json");
using (httpClient)
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpResponseMessage response = httpClient.PostAsJsonAsync(Leads_URI, zohoCRMLeadsFields).Result;
return response.IsSuccessStatusCode;
}
catch (Exception ex)
{
return false;
}
}
}
catch (Exception ex)
{
return false;
}
}
This is code where I am getting the Bad Request Error.
Please help to solved the problem.