2

I have a POST method API which is giving 200 response code in Postman but not when calling api by using Volley or even in Retrofit2.

Here is Postman screenshots:

enter image description here

Here what i did for Volley library code:

final String url = "http://xxxxxxxx.com/api/mobile/user/post/";

    StringRequest stringReq = new StringRequest(Request.Method.POST, url,
            new com.android.volley.Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e("onResponse ===", response + " " );
                }
            },
            new com.android.volley.Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("onErrorResponse ===", error + " " );
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("Authorization", "xxxxxxxxxxxxx");
            return params;
        }

        @Override
        public Map<String, String> getParams() {
            HashMap<String, String> params = new HashMap<>();
            params.put("post_body", "test");
            return params;
        }
    };

    // Adding request to request queue
    mApp.addToRequestQueue(stringReq);
    stringReq.setRetryPolicy(new DefaultRetryPolicy(
            REQUEST_TIME,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Error Logcat:

BasicNetwork.performRequest: Unexpected response code 500 for http://xxxxxxxxxxx.com/api/mobile/user/post/
Viks
  • 1,510
  • 4
  • 22
  • 50
  • can you please share your manifest? Did you add the following permissions? "android.permission.INTERNET" "android.permission.ACCESS_NETWORK_STATE" – Avi Levin Jan 30 '17 at 12:35
  • ya man no doubt on it. – Viks Jan 30 '17 at 12:55
  • 500 is a server error. My experience with these situations is usually related with headers. Postman adds by default some headers that retrofit and volley don't. Sometimes the severs can't cope with this lack of headers and throw a500. I'd advise you to check this. It might be the other way around too. – Fred Jan 30 '17 at 14:21
  • @Fred you are right. We can get response in iOS as well, but not in Android. Strll i am not getting any solution. – Viks Jan 30 '17 at 14:59
  • So what I was suggesting is to verify which headers your Android implementation sends and which ones Postman or iOS sends and find out the one that crashes the server. Once you know that (assuming it's a header) you can add it with retrofit using the ``@HEADER`` annotation or even an ``OkHttp`` interceptor if needed. – Fred Jan 30 '17 at 15:11
  • 1
    You can get working code from postman as it provide under save button – Rajesh Feb 02 '17 at 09:05
  • 1
    Have actually no idea why you're ignoring my answer, seems it solves the problem. – romtsn Feb 02 '17 at 18:14
  • See my answer here: http://stackoverflow.com/questions/41930098/retrofit2-post-method-showing-server-internal-error-but-postman-giving-response/42041502#42041502 – Adhish Feb 06 '17 at 04:49
  • @ErAdhish this is not valid at all as we speak Volley here and with the request he sends contains the content type, however he sends the incorrect one as rom4ek noted. – kalin Feb 06 '17 at 21:48

3 Answers3

7

The problem is that your endpoint assumes multipart/form-data request (as orange pin is set to form-data radiobutton in Postman), while Volley's default content-type for StringRequest is application/x-www-form-urlencoded, that's why you get 500 error.

So, if you're only supposed to send POST params in multipart request, check this answer. But if you want to send files as well, check another answer

Community
  • 1
  • 1
romtsn
  • 11,704
  • 2
  • 31
  • 49
0

Why don't you use JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, JSONObject, response, error); for post request it's easy to use try once

Hemanth S
  • 669
  • 6
  • 16
  • This is not library problem, its headers that your passing and can you check with ION network library for android or check in Postman in response headers it shows 10 while your passing one – Hemanth S Feb 02 '17 at 12:03
-1

I also had the same issue I solved by using

 @Override
 public Map<String, String> getHeaders() throws AuthFailureError {
  final Map<String, String> headers = new HashMap<>();
  headers.put("Content-Type", "application/json");
  return headers;
 }
Dharman
  • 30,962
  • 25
  • 85
  • 135