The REST POST request does not work on Java but works on the REST client

advertisements

Good morning!

I've created a REST service with C# and I'm trying to send a post request from android-studio to the URL. My URL has two methods currently, GET and POST. The GET method works fine, I'm able to consume from android without any problem. But when it comes to the POST request I'm not able to use it from android.

I've tested the POST method using the REST client chrome add-on and it looks like the request works fine, I'm able to reach the method and execute whatever the code needs to execute. Below the evidence:

Request data:

Request response:

Below the C# api:

[RoutePrefix("api/leads")]
    public class LeadsController : ApiController
    {
        // GET: api/Leads/5
        public List<Lead> Get(string matricula, int tipo)
        {
            List<Lead> leads = new List<Lead>();

            //Fills the list

            return leads;
        }

        [HttpPost]
        // POST: api/Leads
        public bool Post(int id, string usuario, string latitude, string longitude)
        {
            //Do stuff

            return true;
        }
}

And last, but not least, the java code that I'm using to the POST request:

@Override
    protected String doInBackground(String... strings) {
        URL url = null;
        try {
            String newUrl = getNewUrl();

            StringBuilder postData = new StringBuilder();

            for (Map.Entry<String,Object> param : params.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
            }

            url = new URL(newUrl);

            byte[] postDataBytes = postData.toString().getBytes("UTF-8");

            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
            conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));

            conn.getOutputStream().write(postDataBytes);

            int responseCode = conn.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK){
                server_response = readStream(conn.getInputStream());
            }

        } catch (MalformedURLException e) {
            listener.onPostResponse(false, e.getMessage(), this.tipoAcesso);
        } catch (UnsupportedEncodingException e) {
            listener.onPostResponse(false, e.getMessage(), this.tipoAcesso);
        } catch (ProtocolException e) {
            listener.onPostResponse(false, e.getMessage(), this.tipoAcesso);
        } catch (IOException e) {
            listener.onPostResponse(false, e.getMessage(), this.tipoAcesso);
        }
        return null;
    }

The URL that the method getNewUrl() return is: "https://cliente.teleatlantic.com.br/CheckinLeads/api/Leads/".

The parameters are: "usuario=FERNANDOCHAVES&latitude=-46.6903502&longitude=-46.6903502&id=265857"

I've looked tirelessly all over the internet (this forum included) for a way of fixing my problem with POST requests. But none of the solutions that I've found solved my problem.

What I've tried so far:
1.Append "?" before parameters sent in the POST request.
2.Append "/CheckinLeads/api/Leads?" before the parameters.
3.Change the code to use HttpClient instead of HttpURLConnection.
4.Change content data to "text/json".
5. Many "ready to use" functions with HttpURLConnection and HttpClient.

I'm new to java/android and any help would be appreciated.
I'm using 4.4 Android KitKat SDK Version 24.
Thanks for the attention! Best regards.

-----EDIT-----
The code doesn't crash. But when I check the HttpURLConnection.getResponseCode of the request it shows me a 404. But the URL and parameters that I sent are the same used to test within the REST client.


Change the api code 

public class test {
public int id {get;set;}
public string usuario {get;set;}
public string latitude {get;set;}
public string longitude { get; set; }
} 

[HttpPost]
// POST: api/Leads
public bool Post(test objTest)
{
//Do stuff return true;
}