Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it OkHttp post method sends get request instead of post request?

I am working in android app that needs to get a token from a web api through POST request, I am using OkHttp library version 2.3.0 but strangely the post method is executing GET request as the result I am getting 405 status code which is method not allowed response from the server.

Here's my code.

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

public String executePOST(){

    OkHttpClient client = new OkHttpClient();

    .
    .
    .
    .

    String url = API_URL + "/oauth/access_token/";
    String strResponse = null;

    RequestBody body = RequestBody.create(JSON, jsonBody);

    Request request = new Request.Builder()
          .url(url)
          .post(body)
          .build();

    Response response;

    try {

        response = client.newCall(request).execute();
        strResponse = response.body().string(); 

      } catch (IOException e) {
            e.printStackTrace();
      }

      return strResponse;
   }

When I debug the code the response object has this content...

enter image description here

Any idea what's the problem with this? What is the possible work around for this one? Your help will be greatly appreciated! Thanks...

like image 820
Ariel Magbanua Avatar asked Dec 10 '25 20:12

Ariel Magbanua


1 Answers

I had this kind of problem when i send POST to http:// and not https://.

I suppose, it automatically redirected me to https:// where the method type was lost.

like image 122
Simon Harvan Avatar answered Dec 13 '25 11:12

Simon Harvan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!