kittinunf / fuel

The easiest HTTP networking library for Kotlin/Android

Home Page:https://fuel.gitbook.io/documentation/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PATCH request received as POST

aemaem opened this issue · comments

Bug Report

Description

When sending a PATCH request to the API server it receives a POST request instead. However, when executing the generated curl command then the request is successful.

To Reproduce

Steps to reproduce the behavior:

  1. Start Grafana docker run --rm -p 3000:3000 -e GF_SECURITY_ADMIN_USER=admin -e GF_SECURITY_ADMIN_PASSWORD=admin grafana/grafana:8.2.5
  2. Execute the Kotlin code below
  3. Grafana logs POST request with status 404 t=2021-12-02T08:28:47+0000 lvl=info msg="Request Completed" logger=context userId=1 orgId=1 uname=admin method=POST path=/api/orgs/1/users/1 status=404 remote_addr=172.17.0.1 time_ms=30 size=24 referer=
  4. The curl command generated by Fuel however works: curl -i -X PATCH -d "{\"role\":\"Admin\"}" -H "Content-Type:application/json" -H "Authorization:Basic YWRtaW46YWRtaW4=" http://localhost:3000/api/orgs/1/users/1
// using Kotlin 1.6.0 with Java 17 and latest Fuel 2.3.1

import com.github.kittinunf.fuel.core.FuelManager
import com.github.kittinunf.fuel.core.extensions.authentication
import com.github.kittinunf.fuel.core.extensions.jsonBody
import com.github.kittinunf.fuel.core.interceptors.LogRequestAsCurlInterceptor
import com.github.kittinunf.fuel.core.interceptors.LogResponseInterceptor
import com.github.kittinunf.fuel.httpPatch

object Main {
    @JvmStatic
    fun main(args: Array<String>) {
        FuelManager.instance.addRequestInterceptor(LogRequestAsCurlInterceptor)
        FuelManager.instance.addResponseInterceptor(LogResponseInterceptor)

        val (req, res, data) = "http://localhost:3000/api/orgs/1/users/1"
            .httpPatch()
            .authentication().basic("admin", "admin")
            .jsonBody("""{"role":"Admin"}""")
            .response()
    }
}

Expected behavior

PATCH request actually sends a PATCH request and not a POST.

Development Machine

  • OS: Arch Linux
  • IDE: IntelliJ
  • Fuel version: 2.3.1
  • Kotlin version: 1.6.0
  • Java version: 17

Smartphone or Emulator

  • Device: -
  • OS: -

This issue appears architectural design and may not have any sort of fix on Fuel's end, and requires work to be done on the server-side if possible, that or the use of another library.

A quote from Baeldung:

Fuel supports all HTTP verbs except for PATCH. The reason is that Fuel’s HttpClient is a wrapper over java.net.HttpUrlConnection which doesn’t support PATCH.

To side-step the problem, the HttpClient converts PATCH requests to a POST request and adds an X-HTTP-Method-Override: PATCH header, so we’ll need to make sure our APIs are configured to accept this header by default.

To get around this myself, I used Square's okhttp4 to send the PATCH request.

Thanks for the clarification!