title: How to write / convert CURL for Android java
body: <p>I am trying to implement the MOT history API <a href="https://dvsa.github.io/mot-history-api-documentation/" rel="nofollow noreferrer">https://dvsa.github.io/mot-history-api-documentation/</a> and they give an example using CURL which works with the supplied api key successfully when using an online CURL tool.</p>
  <p>I am trying to implement this in Android and realise I have to use something like HttpPost rather than CURL, this is my code:</p>
  <pre><code>    //Tried with full URL and by adding the registration as a header.
      //HttpPost httpPost = new HttpPost(&quot;https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests?registration=&quot; + reg_selected);
       HttpPost httpPost = new HttpPost(&quot;https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests&quot;);
  
       httpPost.addHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);
       httpPost.addHeader(&quot;Accept&quot;, &quot;application/json+v6&quot;);
       httpPost.addHeader(&quot;x-api-key&quot;, &quot;abcdefgh123456&quot;);
       httpPost.addHeader(&quot;registration&quot;, 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 = &quot;Bad Request&quot;;
            }else if (response.getStatusLine().getStatusCode() == 403){
                  //Unauthorised – The x-api-key is missing or invalid in the header
                  error_text = &quot;Authentication error&quot;; //&lt;&lt;&lt;&lt; FAILS HERE 403
            }
  </code></pre>
  <p>response.getStatusLine().getStatusCode() returns •  &quot;403 – Unauthorised – The x-api-key is missing or invalid in the header&quot;.
  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.</p>
  <p>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?</p>
  <p>Thanks</p>