valyala/fasthttp

[Bug/Question] Hostclient sends content-type header on DELETE request even if it is not set

Open

#873 opened on Sep 5, 2020

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Go (21,741 stars) (1,755 forks)batch import
help wantedpending/development

Description

Hey, I just started to learn golang as well as fashttp. I tried to build a fasthttp reverse proxy using this libary and later using the code from this issue.

All requests work without any problems, only on delete requests the hostclient adds the header content-type with the value application/x-www-form-urlencoded.

The server to which the request is proxyed responds with 406, is there a way to disable this behavior?

Thank you in advance for any help.

Code example: In the code example I don't use a proxy just the fashttp.Client to make a request with the request method set to DELETE. I have tested the behavior with a fashttp and express server, the code is shown below.

sender.go

package main

import (
	"fmt"

	"github.com/valyala/fasthttp"
)

func main() {
	req := fasthttp.AcquireRequest()

	resp := fasthttp.AcquireResponse()
	req.SetRequestURI("http://localhost:3000/")

	req.Header.SetMethod("DELETE")

	req.Header.Del("content-type")

	client := &fasthttp.Client{}
	fmt.Printf("Content-Type: %s\n", string(req.Header.ContentType()))
	client.Do(req, resp)
	fmt.Printf("Content-Type: %s\n", string(req.Header.ContentType()))

	fmt.Println(resp.StatusCode(), string(resp.Body()))
	fmt.Println(string(resp.Body()))

}

Output:

Content-Type: 
Content-Type: 
200 OK
OK

receiver.go

package main

import (
	"fmt"

	"github.com/valyala/fasthttp"
)

func fastHTTPHandler(ctx *fasthttp.RequestCtx) {

	fmt.Println(string(ctx.Request.Header.Header()))

	fmt.Fprintf(ctx, "Hi there! RequestURI is %q", ctx.RequestURI())

}

func main() {
	fmt.Println("server started...")

	server := &fasthttp.Server{
		Handler: fastHTTPHandler,
	}

	err := server.ListenAndServe(":3000")
	if err != nil {
		panic(err)
	}

}

Output:

DELETE / HTTP/1.1
User-Agent: fasthttp
Host: localhost:3000
Content-Type: application/x-www-form-urlencoded
Content-Length: 0

Express JS Server example server.js

const express = require('express');
const app = express();
const port = 3000;

app.use((req, res, next) => {
  console.log(req.headers);
  res.sendStatus(200);
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

Output:

{
  'user-agent': 'fasthttp',
  host: 'localhost:3000',
  'content-type': 'application/x-www-form-urlencoded',
  'content-length': '0'
}

Contributor guide