-2
  • I can't access json with PUT request on nginx web server, However same request with post request can by accessed!

    String json = "{ \"key\": \"" + value + "\" }";
    
    OkHttpClient  client  = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
    
    client.newCall(new Request.Builder()
                            .url(url)
                            .put(RequestBody.create(JSON, json))
                            .build()
                    ).execute();
    
    
    public static final MediaType JSON
                = MediaType.parse("application/json; charset=utf-8");
    

Update:

curl -i -H "Accept: application/json" -H "token: token" -X PUT -d '{ "uid": 123, "name": "Name",                         }' http://api.myserver.com
  • Response with NOT ALLOWED

HTTP/1.1 405 Not Allowed Server: nginx/1.4.6 (Ubuntu) Content-Type: application/json Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.5.9-1ubuntu4.11 Cache-Control: no-cache Date: Thu, 25 Aug 2016 21:37:02 GMT

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73

1 Answers1

0

After some search i found reason:

  • You need to compile nginx with HttpDavModule Stackoverflow.

  • But i can fix my issue from client side by adding "_METHOD=PUT","PUT" to my json.

  • And convert PUT request to POST.

  • Header("X-HTTP-Method-Override", "PUT").

    String json = "{ \"_METHOD=PUT\":\"PUT\",\"key\": \"value\"\" }";
    
    
    client.newCall(new Request.Builder()
                    .url(url)
                    .post(RequestBody.create(JSON, json))
                    .Header("X-HTTP-Method-Override", "PUT")
                    .build()
            ).execute();
    

Update

  • No need to add "_METHOD=PUT","PUT" to json on body.
  • Just convert PUT request to POST & add Header("X-HTTP-Method-Override", "PUT") made it work.

    curl -i -H "Accept: application/json" -H "-HTTP-Method-Override: PUT" -H "token: token" -X POST -d '{ "uid": 123, "name": "Name"}' http://api.myserver.com
    

HTTP/1.1 200 OK Server: nginx/1.4.6 (Ubuntu) Content-Type: application/json Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.5.9-1ubuntu4.11 Cache-Control: no-cache Date: Thu, 25 Aug 2016 21:37:02 GMT

Community
  • 1
  • 1
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73