Home > Software design >  How to write / convert CURL for Android java
How to write / convert CURL for Android java

Time:08-20

I am trying to implement the MOT history API https://dvsa.github.io/mot-history-api-documentation/ and they give an example using CURL which works with the supplied api key successfully when using an online CURL tool.

I am trying to implement this in Android and realise I have to use something like HttpPost rather than CURL, this is my code:

    //Tried with full URL and by adding the registration as a header.
    //HttpPost httpPost = new HttpPost("https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests?registration="   reg_selected);
     HttpPost httpPost = new HttpPost("https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests");

     httpPost.addHeader("Content-Type", "application/json");
     httpPost.addHeader("Accept", "application/json v6");
     httpPost.addHeader("x-api-key", "abcdefgh123456");
     httpPost.addHeader("registration", reg_selected);

     StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
     httpPost.setEntity(entity);
     HttpClient client = new DefaultHttpClient();

     try {
          HttpResponse response = client.execute(httpPost);

          if (response.getStatusLine().getStatusCode() == 200) {

               InputStream inputStream = response.getEntity().getContent();
               bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
               String readLine = bufferedReader.readLine();

               String jsonStr = readLine;
               JSONObject myJsonObj = new JSONObject(jsonStr);

               
          }else if (response.getStatusLine().getStatusCode() == 400){
                 //Bad Request  Invalid data in the request. Check your URL and parameters
                 error_text = "Bad Request";
          }else if (response.getStatusLine().getStatusCode() == 403){
                //Unauthorised – The x-api-key is missing or invalid in the header
                error_text = "Authentication error"; //<<<< FAILS HERE 403
          }

response.getStatusLine().getStatusCode() returns • "403 – Unauthorised – The x-api-key is missing or invalid in the header". However the x-api-key that I use works correctly with the online CURL test so the actual key is correct but how I am adding it to my android code request must be invalid or similar.

Can anyone throw any light as to the correct way to convert the CURL into Android java so that the server does not return 403?

Thanks

CodePudding user response:

It's easy to do with Jsoup:

    // CREATE CONNECTION
    Connection conn=Jsoup.connect("URL_GOES_HERE");
    
    // ADD POST/FORM DATA
    conn.data("KEY", "VALUE");
    
    // ADD HEADERS HERE
    conn.header("KEY", "VALUE");
    
    // SET METHOD AS POST 
    conn.method(Connection.Method.POST);
    
    // ACCEPT RESPONDING CONTENT TYPE
    conn.ignoreContentType(true);
    
    try
    {
        // GET RESPONSE
        String response = conn.execute().body();
        
        // USE RESPONSE HERE
        // CREATE JSON OBJECT OR ANYTHING...
    } catch(HttpStatusException e)
    {
        int status = e.getStatusCode();
        // HANDLE HTTP ERROR HERE
    } catch (IOException e)
    {
        // HANDLE IO ERRORS HERE
    }

Ps: I guess you are confused with Header and Post Data. The key etc (Credentials) must be used as Post Data and Content Type etc as Header.

  • Related