radioaktiv
radioaktiv

Reputation: 2539

Android Cannot POST /api response

I am trying to do HTTP post request in a REST backend. The URL for the backend is using SSL therefore I have also added the necessary code to handle that. But I got the following response:

Cannot POST /api

Here is my code:

protected String doInBackground(String... args) {
                try {

                HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
                DefaultHttpClient client = new DefaultHttpClient();
                SchemeRegistry registry = new SchemeRegistry();
                SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
                socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
                registry.register(new Scheme("https", socketFactory, 443));
                SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
                DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());



                HttpPost post = new HttpPost("https://test-api.smart-trial.dk/api");
                List <NameValuePair> nvps = new ArrayList <NameValuePair>(2);
                nvps.add(new BasicNameValuePair("path[pathwayId]","5566e151817a62021b1ea809"));
                nvps.add(new BasicNameValuePair("formData[firstname]","Name"));
                nvps.add(new BasicNameValuePair("formData[lastname]","XXX"));
                nvps.add(new BasicNameValuePair("formData[email]","[email protected]"));

                 post.addHeader("Referer" ,"https://myurl.com/api");
                 post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));


                //DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpResponse response = httpClient.execute(post);
                HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        // get the response content as a string
                        String stringResponse = EntityUtils.toString(entity);

                        Log.d("Response", stringResponse);
                    }

Anything wrong in the code ? Or at least why do I get that response.

EDIT

I also have some rules for the paramaters.

"The post should be done with x-www-form-urlencoded body parameters"
I think more or less I over that by using post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); which x-www-form-urlencoded.

Parameters:

  1. pathwayId (should be located in path)
  2. firstname (should be located in formData)
  3. lastname (should be located in formData)
  4. email (should be located in formData)
  5. Referer (Should be located in Header)

Upvotes: 1

Views: 1001

Answers (1)

kris larson
kris larson

Reputation: 30985

Sounds like your server is not configured to allow POST requests to that URL. But you need a way to verify that.

If you don't already have a REST testing plugin for your browser, find a plugin that will allow you to enter POST request data, download it and install it.

Then duplicate the POST data in the browser plugin, submit the request and view the response from the server.

At least this should help you figure out if the problem is in the app or in the server.

Upvotes: 1

Related Questions