Skip to content

Instantly share code, notes, and snippets.

@aichaoxy
Last active October 23, 2023 09:00
Show Gist options
  • Save aichaoxy/484f4edcfb715b0768796753f76ed0cf to your computer and use it in GitHub Desktop.
Save aichaoxy/484f4edcfb715b0768796753f76ed0cf to your computer and use it in GitHub Desktop.
Golang Is Shit E002
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
type BodyPrinter struct {
}
func (ctr *BodyPrinter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
body, _ := ioutil.ReadAll(req.Body)
resp := string(body)
_, _ = fmt.Fprintf(w, "body is\n%s\nlength is %d", resp, len(resp))
}
func Run() {
http.Handle("/url", new(BodyPrinter))
err := http.ListenAndServe(":8090", nil)
if err != nil {
panic(err)
}
}
#### slash slash ❌
curl --location 'localhost:8090//url' \
--header 'Content-Type: application/json' \
--data '{}'
body is
length is 0 ## why?
#### slash ✅
$ curl --location 'localhost:8091/url' \
--header 'Content-Type: application/json' \
--data '{}'
body is
{}
length is 2
package com.example;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
@Controller("/")
public class HelloController {
@Get(uri = "/hello/get", produces = MediaType.TEXT_PLAIN) // (2)
public String index() {
return "Hello World";
}
@Post(uri = "/hello/post", consumes = MediaType.APPLICATION_JSON)
public String process(@Body String formInput) {
return String.format("Hello json body len=%d", formInput.length());
}
}
#### slash slash ✅
curl --location 'localhost:8090/hello//post' \
--header 'Content-Type: application/json' \
--data '{}'
{"_links":{"self":[{"href":"/hello//post","templated":false}]},"_embedded":{"errors":[{"message":"Page Not Found"}]},"message":"Not Found"}
#### slash ✅
$ curl --location 'localhost:8091/hello/post' \
--header 'Content-Type: application/json' \
--data '{}'
Hello json body len=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment