Retrofit2 sending PUT request method invalid for PHP

I am trying to send a PUT request method from my Android application to a PHP endpoint, but at my endpoint, the PUT request is not recognized as a PUT request, so I am returning the request method incorrectly! . from my endpoint.

Android interface and query execution

Interface to activate

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

Query execution

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

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

response = registryCallback.execute();

With this, I expect an answer, but I get an 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 it $_SERVER['REQUEST_METHOD'] == "PUT"is false, but I wonder if something is missing on Retrofit 2.


Additional Information.

I am using Retrofit2.


Update 1: sending json to body

I am trying to send json using body.

This 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);

.


2:

REstfull:

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

? , ?

3: .

. PUT/POST. , , .

, (PUT/POST), php GET? ( POST, )

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

- POST:

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

POST ( , PUT) , GET. !!! , .

4: godaddy.

php- godaddy. ? , , godaddy. , , , Godaddy ?

+4
4

PHP , GET POST. - , .

PUT

$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);

, .

+2

, , - PUT, PHP GET. , @FormUrlEncoded, @Multipart , , @Body

+1

retrofit2, :

        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());
            }
        };

:

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

'REQUEST_METHOD' ; "GET", "HEAD", "POST", "PUT".

  1. - , - .

Before using Retrofit or any other network library, you must verify the endpoint using an HTTP request client, such as Postman or Advanced Client for recreation . To debug a request / response when starting your application or unit tests, use a proxy server, for example Charles , this will help you learn a lot about how your request / response really looks.

+1
source

All Articles