HTTP POST method returning status code 404

41,796

Solution 1

from wiki:

The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with the server, but the server could not find what was requested.

so, your code is OK, but server cannot find resource you are looking for. Double check if your url is correct.


how to pass request through fiddler proxy for debugging purposes:

  HttpParams params = new BasicHttpParams();

  // ....

  HttpHost proxy = new HttpHost("192.168.1.12", 8888); // IP to your PC with fiddler proxy
  params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

  // use params as a second parameter to: following constructor:
  // public DefaultHttpClient (ClientConnectionManager conman, HttpParams params) 

Solution 2

I was getting 404 for POST requests because mod_headers module of Apache 2 server was not enabled. If that happens you can enable it with:

sudo a2enmod headers

and then restart apache:

sudo service apache2 restart
Share:
41,796
Braj
Author by

Braj

Nothing...!

Updated on December 25, 2020

Comments

  • Braj
    Braj over 3 years

    When I execute an API through following method, I always get 404 as response code.

    private void execute() throws IllegalStateException, IOException, NoSuchAlgorithmException {
    
        Map<String, String> comment = new HashMap<String, String>();
        comment.put("accounts-groups", "customers/enterprise");
        comment.put("companyType", "customer");
        comment.put("companyName", "Test");
        String json = new GsonBuilder().create().toJson(comment, Map.class);
        Log.i(TAG, "json : "+json);
    
        HttpResponse response = makeRequest(URL, json);
    
        /*Checking response */
        if(response != null) {
            InputStream inputStream = response.getEntity().getContent(); //Get the data in the entity
            int statusCode = response.getStatusLine().getStatusCode();
            Log.i(TAG, "statusCode : "+statusCode);
            String result;
            // convert inputstream to string
            if(inputStream != null)
                result = convertStreamToString(inputStream);
            else
                result = "Did not work!";
    
            Log.i(TAG, "result : "+result);
        }
    }
    
    private HttpResponse makeRequest(String uri, String json) throws NoSuchAlgorithmException {
        Log.i(TAG, "uri : "+uri);
        try {
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(new StringEntity(json, HTTP.UTF_8));
    
            long timestamp = System.currentTimeMillis();
    
            String signatureKey = PRIVATE_KEY + timestamp;
    
            byte[] bytesOfMessage = signatureKey.getBytes(HTTP.UTF_8);
    
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] thedigest = md.digest(bytesOfMessage);
            char[] signature = Hex.encodeHex(thedigest);
    
            String finalSignature = String.valueOf(signature);
    
            Log.i(TAG, "finalSignature : "+finalSignature);
    
            httpPost.setHeader("Timestamp", ""+timestamp);
            httpPost.setHeader("Api_token", API_TOKEN);
            httpPost.setHeader("Signature" , finalSignature);
    
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");          
    
            return new DefaultHttpClient().execute(httpPost);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    I am not getting where am I going wrong. Can anybody please help me out?

  • Braj
    Braj over 10 years
    url is correct. Same url they r using in apex code and its working fine. Don't know what's wrong in java code.
  • marcinj
    marcinj over 10 years
    I would debug this request using fiddler on windows, that would show exactly what is going to server, and what is returning - also this allows to save such request and test it on desktop.
  • Braj
    Braj over 10 years
    1st parameter to HTTPHost is my system IP address and can please tell me what is "params" n how to get it? am on ubuntu
  • marcinj
    marcinj over 10 years
    updated answer on how to set params, unfortunately I am not aware of any Fiddler2 alternatives on linux
  • Braj
    Braj over 10 years
    There was some problem in server so for everything it was responding 404.