2

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...

cwhsu
  • 1,493
  • 24
  • 31
  • Did you check the wire to see what you're actually sending? – Dave Newton Aug 06 '13 at 13:45
  • I don't really understand what you're trying to say... I do try to read the entity by putting it back to a InputStreamReader and then BufferedReader. The string of entity itself is correct. – cwhsu Aug 06 '13 at 13:52
  • I'm saying "check your assumptions regarding what's actually being sent to their server by checking the request being made". – Dave Newton Aug 06 '13 at 13:54
  • I do use the correct api key in my code. I don't put my api key in this demo code if that is what you're suggesting. Otherwise, I still have no idea what you're trying to tell me. – cwhsu Aug 06 '13 at 14:09
  • 1
    I'm trying to tell you to examine the actual request being made by checking what's actually being sent over the network; Debugging 101. – Dave Newton Aug 06 '13 at 14:19
  • Ok, I try to capture the request by wire shark. The request is a POST request, and the data is text-plain **json={"contacts": [ { "email": "doug@fliptop.com", "title":"CEO", "company":"Fliptop", "address":"San Francisco"} ] }**. I am not an expert of network session, but it seems fine to me. – cwhsu Aug 06 '13 at 15:01
  • http://stackoverflow.com/questions/12059278/how-to-post-json-request-using-apache-httpclient-3-1 – Dave Newton Aug 06 '13 at 16:28
  • @DaveNewton The api in the link is way too old, I cannot even find the apache httpclient 3.1 to try it. I found this post http://stackoverflow.com/questions/14119410/json-not-working-with-httppost-probably-around-setentity and made some changes. However, it's still not working. – cwhsu Aug 06 '13 at 17:18

1 Answers1

1

After struggling for a few more hours, I discover the cause of my issue by examining details of the post request from chrome. You can see the Content-Type is actually "application/x-www-form-urlencoded", and that's why I keep failing. By changing my Content-Type, everything works fine.

enter image description here

Here are my codes, you can use the URL or the appache httpclient, they both work fine.

    URL url = new URL("http://api.fliptop.com/s2/v1/find_contact?api_key=xxxxxxxxxxxxxxxxxxxxxx");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(data);  // data = "json={\"contact\": { \"email\": \"robbie@fliptop.com\"}}"
    writer.close();

    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {        
        BufferedReader reader =
            new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF_8));

        String line;
        while ((line = reader.readLine()) != null) {
            result += line;
        }       
    }
cwhsu
  • 1,493
  • 24
  • 31