Robert
Robert

Reputation: 10953

retrofit2 sending a PUT request method is wrong for PHP

I am trying to send a PUT request method from my Android app to my PHP endpoint but in my endpoint the PUT request is not recognized as a PUT request so I return Request method is wrong! message from my endpoint.

Android interface and request execution

Interface for activation

@PUT("device/activate.php")
Call<DeviceRegistry> registryDevice();

Executing the request

DeviceRegistryAPI registryAPI =
            RetrofitController.getRetrofit().create(DeviceRegistryAPI.class);

Call<DeviceRegistry> registryCallback = registryAPI.registryDevice();

response = registryCallback.execute();

With this I am expecting a response but I am getting my endpoint error message.


My PHP endpoint

if($_SERVER['REQUEST_METHOD'] == "PUT"){
   //doing something with the data
} else {
    $data = array("result" => 0, "message" => "Request method is wrong!");
}

I don't know why the $_SERVER['REQUEST_METHOD'] == "PUT" is false but I wonder if I am missing something on Retrofit 2.


More Info.

I am using Retrofit2.


Update 1: Sending json into the body

I am trying to send a json using the body.

It is my json:

{
    "number": 1,
    "infoList": [
        {
            "id": 1,
            "info": "something"
        },
        {
            "id": 2,
            "info": "something"
        }   
    ]
}

There are my classes:

class DataInfo{

    public int number;  
    public List<Info> infoList;

    public DataInfo(int number, List<Info> list){
        this.number = number;
        this.infoList = list;
    }
}


class Info{
    public int id;
    public String info;
}

I changed the PUT interface to this:

@PUT("device/activate.php")
Call<DeviceRegistry> registryDevice(@Body DataInfo info);

But I am getting the same problem.


Update 2: Do I need Header

I have this header in my REstfull client:

Accept: application/json
Content-Type: application/x-www-form-urlencoded

Do I need to put this on my request configuration? How do I do that if I need it?

Update 3: checking the request type of my sending post.

Now I am checking the type of the request. Because I am having the same problem with the PUT/POST requests. So If can solved the problem with the put maybe all the problems will be solved.

When I execute the request and asking and inspect the request it is sending the the type (PUT/POST) but in the server php only detect or GET?? (the below example is using POST and the behavior is the same)

Call<UpdateResponse> requestCall = client.updateMedia(downloadItemList);
Log.i("CCC", requestCall .request().toString());

And the output is a POST:

Request{method=POST, url=http://myserver/api/v1/media/updateMedia.php, tag=null}

so I am sending a POST (no matter if I send a PUT) request to the sever but why in the server I am receiving a GET. I am locked!!! I don't know where is the problem.

Update 4: godaddy hosting.

I have my php server hosting on godaddy. Is there any problem with that? I create a local host and everything works pretty good but the same code is not working on godaddy. I did some research but I didn't find any good answer to this problem so Is possible that godaddy hosting is the problem?

Upvotes: 4

Views: 1279

Answers (4)

V&#237;ctor Albertos
V&#237;ctor Albertos

Reputation: 8303

  1. PHP recognises 'PUT' calls. Extracted from PHP.net:

'REQUEST_METHOD' Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'.

  1. You don't need to send any header if your server isn't expecting any header.

Prior to use Retrofit or any other networking library, you should check the endpoint using a request http builder, like Postman or Advanced Rest Client. To debug the request/response when running your app or unit tests use a proxy like Charles, it will help you a lot to watch how your request/response really looks.

Upvotes: 1

Federico De Gioannini
Federico De Gioannini

Reputation: 927

To add header in your retrofit2 you should create an interceptor:

        Interceptor interceptor = new Interceptor() {
            @Override
            public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException
            {
                okhttp3.Request.Builder ongoing = chain.request().newBuilder();
                ongoing.addHeader("Content-Type", "application/x-www-form-urlencoded");
                ongoing.addHeader("Accept", "application/json");

                return chain.proceed(ongoing.build());
            }
        };

and add it to your client builder:

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.interceptors().add(interceptor);

Upvotes: 1

Nereo Costacurta
Nereo Costacurta

Reputation: 8541

PHP doesn't recognize anything other than GET and POST. the server should throw at you some kind of error like empty request.

To access PUT and other requests use

$putfp = fopen('php://input', 'r'); //will be a JSON string (provided everything got sent)
$putdata = '';
while($data = fread($putfp, filesize('php://input')))
    $putdata .= $data;
fclose($putfp);
//php-like variable, if you want
$_PUT = json_decode($putdata);

did not tested, but should work.

Upvotes: 2

romtsn
romtsn

Reputation: 12002

I guess the problem is that you don't pass any data along with PUT request, that's why PHP recognizes the request as a GET. So I think you just need to try to pass some data using @FormUrlEncoded, @Multipart or probably @Body annotations

Upvotes: 1

Related Questions