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?
$_SERVER['REQUEST_METHOD']to the client and see what is the request method