2

I am calling delete api using retrofit2 it is working in postman properly but giving error in application as below

Response{protocol=http/1.0, code=405, message=METHOD NOT ALLOWED, url=http://192.168...

here is my base url

public static final String BASE_URL = "http://192.168.1.127:3222/";

@DELETE("student/{firstname}/{lastname}")
Call<ResponseBody> deleteStudent(@Path("firstname") String firstname, @Path("lastname") String lastname);

and java file i'm calling it like

Call<ResponseBody> call = interfaces.deleteStudent(FirstName,LastName);```

Postman screenshot here

Bruno
  • 3,204
  • 4
  • 14
  • 30
yuvrajsinh
  • 1,162
  • 4
  • 14
  • post full error stack please? also show your base url – Vir Rajpurohit Sep 17 '19 at 05:50
  • https://stackoverflow.com/questions/6272381/android-httppost-returns-error-method-not-allowed answer already given here please check . "405 method not allowed error in android please this keyword to get your answer " – Yogesh Borhade Sep 17 '19 at 05:52
  • no it is not reated to my error i,m getting this error in delete api only where my url stays same for all @YogeshBorhade – yuvrajsinh Sep 17 '19 at 06:03

5 Answers5

2

Updated thanks for your referance @GovindPrajapati i have to make little changes add @Field and it is working for me.

@FormUrlEncoded
    @HTTP(method = "DELETE", path = "student",hasBody = true)
    Call<ResponseBody> deleteStudent(@Field("firstname") String firstname, @Field("lastname") String lastname);
yuvrajsinh
  • 1,162
  • 4
  • 14
0

You may miss the "/" in base URL

String baseURL= "http//www.example.com/"

http//www.example.com - This will not work in your case

Ranjith Kumar
  • 13,385
  • 9
  • 95
  • 126
0

I think You should set your service url @DELETE("here") like "http.example.com/api/login".

AkashToSky
  • 123
  • 11
0
@FormUrlEncoded
@HTTP(method = "DELETE", path = "student/{firstname}/{lastname}")
Call<ResponseBody> deleteStudent(@Field("firstname") String firstname, @Field("lastname") String lastname);

if above is not working then try this

@FormUrlEncoded
@HTTP(method = "DELETE", path = "student/{firstname}/{lastname}",hasBody = true)
Call<ResponseBody> deleteStudent(@Field("firstname") String firstname, @Field("lastname") String lastname);

Or you can refer this answer as well How to send a HTTP-delete with a body in retrofit?

Govind Prajapati
  • 198
  • 2
  • 10
0

According to postman @FormUrlEncoded method not allowed because data is sent to api in the form of form-data so please use

@Multipart
@DELETE("/student")
Call<MemberListResponse> deleteStudent(@Part ("firstname") RequestBody first_name,
                                   @Part ("lastname") RequestBody last_name);

And here how to get requestBody from String.

 String somevalue = "somevalue";
    RequestBody FirstName= RequestBody.create(MediaType.parse("text/plain"), somevalue);

String somevalue1 = "somevalue";
    RequestBody LastName= RequestBody.create(MediaType.parse("text/plain"), somevalue1);

Call<ResponseBody> call = interfaces.deleteStudent(FirstName,LastName);
Divyanshu
  • 434
  • 6
  • 16