Reputation: 3792
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost request = new HttpPost("https://invoice.zoho.com/api/v3/contacts?authtoken="ur_token"&organization_id="ur_org_id");
StringEntity params =new StringEntity("JSONString={\"contact_name\":\"company_name\",\"age\":\"20\"} ");
request.addHeader("content-type", "application/json");
request.addHeader("Accept","application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
System.out.println(response);
// handle response here...
}catch (Exception ex) {
// handle exception here
} finally {
httpClient.getConnectionManager().shutdown();
}
How do you add a new Customer in ZOHO INVOICE using API? I use the following POST
method to create the new Customer, but it doesn't work.
Upvotes: 0
Views: 1940
Reputation: 73
Could you please ensure that the JSONString that you are providing is in the correct format?
Valid JSON -
{
"contact_name":"Sabari",
"company_name": "Bowman and Co",
"contact_persons": [
{
"salutation": "Mr.",
"first_name": "Will",
"last_name": "Smith",
"email": "[email protected]"
}
]
}
You will face an error if it's not a well formed json format Example -
{customer_name=sabari,company_name=Bowman and Co}
Hope this helps.
Do let me know if you face any issues.
Sabari
Upvotes: 1
Reputation: 4948
Clearly mentions that the JSON you provide is invalid. You can use a JSONSimple library to may be create a simple JSON or GSON as mentioned in the link below and give it a try. https://www.zoho.com/invoice/api/v3/contacts/#create-a-contact
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class ZOHOTestClient {
public static void main(String[] args) throws Exception {
String url = "https://invoice.zoho.com/api/v3/contacts?authtoken=xxxxxxxxxxx6&organization_id=xxxxxxxx";
HttpClient client = HttpClientBuilder.create().build();
JSONParser parser = new JSONParser();
//being lazy pick the JSON from a file :)
Object obj = parser.parse(new FileReader("C:\\DevelopmentTools\\3.CODE\\invoice.json"));
/*
//You can create the JSON like this
JSONObject jsonObject = (JSONObject) obj;
JSONObject zohoInvoiceRequest = new JSONObject();
zohoInvoiceRequest.put("contact_name", "Bowman and Co");
zohoInvoiceRequest.put("company_name", "Bowman and Co");
zohoInvoiceRequest.put("payment_terms", new Integer(15));
zohoInvoiceRequest.put("payment_terms", "Net 15");
JSONObject billing_address = new JSONObject();
billing_address.put("address", "4900 Hopyard Rd, Suite 310");
billing_address.put("city", "Pleasanton");
billing_address.put("state", "94588");
billing_address.put("country", "USA");
zohoInvoiceRequest.put("billing_address", billing_address);
System.out.println(jsonObject.toJSONString());
*/
url += "&JSONString=";
url += URLEncoder.encode(jsonObject.toJSONString(),"UTF-8");
System.out.println(url);
HttpPost post = new HttpPost(url);
post.addHeader("Accept", "application/json");
post.addHeader("charset", "UTF-8");
post.addHeader("Content-Type", "application/json");
// post.setEntity(new StringEn);
// post.setEntity(new StringEntity("&JSONString=" +
// jsonObject.toJSONString()));
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine());
System.out.println("Response Code : " + response);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
}
}
Response Code : HttpResponseProxy{HTTP/1.1 201 Created [Server: ZGS, Date: Fri, 05 Aug 2016 16:32:17 GMT, Content-Type: application/json;charset=UTF-8, Transfer-Encoding: chunked, Connection: keep-alive, Set-Cookie: edfb7d8656=ce40e27c37c608027f2a04de26189092; Path=/; Secure; HttpOnly, X-XSS-Protection: 1, X-Content-Type-Options: nosniff, Set-Cookie: zbcscook=1c083c08-9c4e-4210-9a70-b248751cf5a2; Path=/; Secure, Pragma: no-cache, Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0, Expires: Thu, 01 Jan 1970 00:00:00 GMT, Set-Cookie: JSESSIONID=0A8CC1842C061BC3CA8FD333FEBB5823; Path=/; Secure; HttpOnly, X-Rate-Limit-Limit: 1000, X-Rate-Limit-Reset: 7062, X-Rate-Limit-Remaining: 991, Content-Disposition: attachment;, Allow: OPTIONS, POST, GET, DELETE, Set-Cookie: BuildCookie_632436810=2; Expires=Fri, 05-Aug-2016 16:37:17 GMT; Secure, BUILD_VERSION: Jul_28_2016_2_5435, CLIENT_BUILD_VERSION: Jul_28_2016_2_5435, SERVER_BUILD_VERSION: Aug_02_2016_5_4512/, Location: /api/v3/contacts/xxxxxxxxxxxxxx001, Vary: Accept-Encoding, Cache-Control: no-cache, Strict-Transport-Security: max-age=15768000] org.apache.http.client.entity.DecompressingEntity@d6da883}
The file invoice.json is a copy paste of the JSON request in the URL (https://www.zoho.com/invoice/api/v3/contacts/#create-a-contact)
Upvotes: 0
Reputation: 38
I hope this following piece of code can serve your need.
HttpClient httpClient = HttpClientBuilder.create().build();
String url = "https://invoice.zoho.com/api/v3/contacts?authtoken=AUTHTOKEN&organization_id=ORGID";
HttpPost request = new HttpPost(url);
JSONObject json = new JSONObject();
json.put("contact_name", "Test");
//You can add other JSONString params here.
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("JSONString", json.toString()));
//You can add other params like JSONString here.
request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
request.addHeader("Accept", "application/json");
HttpResponse response = httpClient.execute(request);
ResponseHandler<String> handler = new BasicResponseHandler();
System.out.println(response.getStatusLine().getStatusCode());
String body = handler.handleResponse(response);
System.out.println(body);
Upvotes: 1