10000 POST request with x-www-form-urlencoded is failing as Bad Request · Issue #466 · unjs/ofetch · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

POST request with x-www-form-urlencoded is failing as Bad Request #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ssahillppatell opened this issue Jan 8, 2025 · 4 comments · May be fixed by #483
Open

POST request with x-www-form-urlencoded is failing as Bad Request #466

ssahillppatell opened this issue Jan 8, 2025 · 4 comments · May be fixed by #483
Labels
bug Something isn't working

Comments

@ssahillppatell
Copy link
ssahillppatell commented Jan 8, 2025

Environment

bun: 1.1.38
node: 18.17.0
ofetch: 1.4.1

Reproduction

I have a simple api that accepts a name and return a json response.
Steps:

  1. GO api to handle the request
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/hello", helloHandler)
	fmt.Println("Server is running on port 8080...")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
		return
	}

	err := r.ParseForm()
	if err != nil {
		http.Error(w, "Unable to parse form data", http.StatusBadRequest)
		return
	}

	name := r.FormValue("name")
	if name == "" {
		http.Error(w, "Name field is required", http.StatusBadRequest)
		return
	}

	response := map[string]string{"hello": name}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)

	json.NewEncoder(w).Encode(response)
}
  1. Run via go run app.go
  2. Try using the endpoint via
import { ofetch } from "ofetch";

const options = {
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body: new URLSearchParams({
      name: 'abc'
    })
  };
  
  ofetch('http://localhost:8080/hello', options)
    .then(response => console.log(response))
    .catch(err => console.error(err));

It should error out.

Describe the bug

Form encoded post requests are not working as expected and erroring out(400).

Additional context

I tried using simple fetch and things were working as expected.

const options = {
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body: new URLSearchParams({
      name: 'abc'
    })
  };
  
  fetch('http://localhost:8080/hello', options)
    .then(response => response.json())
    .then(response => console.log(response))
    .catch(err => console.error(err));

Even curl was working:

curl --request POST http://localhost:8080/hello --header 'Content-Type: application/x-www-form-urlencoded' --data name=abc

Logs

FetchError: [POST] "http://localhost:8080/hello": 400 Bad Request
@acidjazz
Copy link

try finishing off URLSearchParams with .toString()

body: new URLSearchParams({ 
  name: 'abc',
}).toString()

@kricsleo
Copy link
Member
kricsleo commented Mar 27, 2025

I tried your example, it works fine. And I'm using ofetch@1.4.1 & node@18.17.0 (same as yours)

Could you please confirm? Feel free to reopen this if you have any futher questions ❤️

Image

@nivseb
Copy link
nivseb commented Apr 6, 2025

I have the same issue. But it looks like that this is an issue with bun, not with ofetch => oven-sh/bun#18805

@kricsleo kricsleo reopened this Apr 6, 2025
@kricsleo
Copy link
Member
kricsleo commented Apr 6, 2025

Confirmed that bun added toJSON method to URLSearchParams which confused ofetch's isJSONSerializable

Another related issue #439 shows that bun also added toJSON to FormData, and Bun won't change this temporarily

So we might have to handle it on our side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0