4

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?

2
  • return $_SERVER['REQUEST_METHOD'] to the client and see what is the request method Commented Jul 9, 2016 at 17:08
  • @shayanpourvatan I did it, but the server return GET, I don't know why? Commented Jul 9, 2016 at 17:30

4 Answers 4

2
+25

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

I changed the request but the problem is the same. I don't know what am I doing wrong.
Hm... I'm not an expert in PHP, but could it cause the problem that you use double quotes instead of single for method type? I mean maybe you should change condition to the following: $_SERVER['REQUEST_METHOD'] == 'PUT'.
I did a test sending back the result of $_SERVER['REQUEST_METHOD'] and its value was GET. So I think that the problem is with retrofit2, well no with retrofit2 but with my configuration. I don't know if I need to send or configure something else. I am locked here.
Maybe you did that test before you've changed your Android code to the latest, which passes some data in PUT request?
Do you have any piece of code that uses retrofit2 and send a PUT/POST request? I am pretty sure that I am doing something wrong or missing something in the request configuration but I don't know what. I keep searching.
1

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

Comments

1
  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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.