I am currently trying to use the API by following the instructions of fliptop. The description of the api specifies that it has to use json input, Hence, I make the json object and convert it to string myself. However, when I try to use the find_contact api, the http response gets
{"param":500}
instead of correct data like
{ "account": { "site_key": "...", "seat_id": "...", "org_id": "", "license": { ... }.
I try to do some tests on documentation website(click any of advanced api and you will see it) by sending request with incorrect input, and it gives me the same response body {"param":500}. Hence, I think there might be something wrong with my request entity.
Here is my code. Can anyone tell me what's wrong with it?
import java.io.BufferedReader;
import java.io.InputStreamReader;
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.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class Test {
// Generate json string
// {"contacts": [ { "email": "doug@fliptop.com", "title":"CEO", "company":"Fliptop", "address":"San Francisco"} ] }
public static String jsonString() {
JSONObject obj1 = new JSONObject();
obj1.put("email", "doug@fliptop.com");
obj1.put("title", "CEO");
obj1.put("company", "Fliptop");
obj1.put("address", "San Francisco");
JSONArray list1 = new JSONArray();
list1.add(obj1);
JSONObject obj2 = new JSONObject();
obj2.put("contacts", list1);
System.out.println(obj2.toJSONString());
return obj2.toJSONString();
}
public static void find_contact() {
HttpClient httpClient = new DefaultHttpClient();
try {
//you will need api key here!!
HttpPost request = new HttpPost("http://api.fliptop.com/s2/v1/find_contact?api_key=xxxxxxxxxxxxxxxxxxxxxx");
StringEntity params = new StringEntity(jsonString(),"UTF-8");
params.setContentType("application/json; charset=UTF-8");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String result = "";
String line;
while ((line = rd.readLine()) != null) {
result += line;
}
System.out.println(result);
rd.close();
System.out.println("status:" + response.getStatusLine());
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
find_contact();
}
}
Update:
I follow Dave's suggestion and check the request by WireShark. The request is showed as a POST request, and the data is text/plain with the following contents
json={"contacts": [ { "email": "doug@fliptop.com", "title":"CEO", "company":"Fliptop", "address":"San Francisco"} ] } .
I'm not a network expert, but the request seems fine to me. Still, I confused about what's going on...