echo icon indicating copy to clipboard operation
echo copied to clipboard

How to implement endpoints for specific content-types

Open eloo opened this issue 5 years ago • 5 comments

Issue Description

How to implement endpoints for specific content-types

Checklist

  • [x] Dependencies installed
  • [x] No typos
  • [x] Searched existing issues and docs

Expected behaviour

I've have an PATCH endpoint which only accepts requests with content-type application/merge-patch+json

Actual behaviour

Not sure how to implement it

Details

I'm currently trying to create an endpoint which supports the https://tools.ietf.org/html/rfc7396 standard for patching resources. But im struggling right now how to implement the specific content-type i want to accept. So every request without the content-type to be set to application/merge-patch+json should be rejected with at 415.

My two ideas are the following:

  • In the handler function i just check the header manually and reject it with 415 if the content-type does not match or what i've found here so far
  • Create custom binder to bind the data which only accepts the specific content-type

I guess the second approach is the better but i'm not sure if there is maybe another solution or what should be prefered?

Thanks

eloo avatar Jun 04 '20 17:06 eloo

think that you should check both

mymtw avatar Jun 10 '20 00:06 mymtw

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 09 '20 01:08 stale[bot]

for the record: i'm now using the following snippet for this use-case:

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"

	"github.com/labstack/echo/v4"
)

type MergeJsonBinder struct{}

// Bind implements the `Binder#Bind` function.
func (b *MergeJsonBinder) Bind(i interface{}, c echo.Context) (err error) {
	req := c.Request()

	names := c.ParamNames()
	values := c.ParamValues()
	params := map[string][]string{}
	for i, name := range names {
		params[name] = []string{values[i]}
	}
	if req.ContentLength == 0 {
		return
	}
	ctype := req.Header.Get(echo.HeaderContentType)
	switch {
	case strings.HasPrefix(ctype, MIMEApplicationMergePatchJSON):
		if err = json.NewDecoder(req.Body).Decode(i); err != nil {
			if ute, ok := err.(*json.UnmarshalTypeError); ok {
				return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset)).SetInternal(err)
			} else if se, ok := err.(*json.SyntaxError); ok {
				return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: offset=%v, error=%v", se.Offset, se.Error())).SetInternal(err)
			}
			return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
		}
	default:
		return echo.ErrUnsupportedMediaType
	}
	return
}

and something like this in the endpoint handler

mergeJsonBinder := MergeJsonBinder{}
		var body map[string]interface{}
		err := mergeJsonBinder.Bind(&body, c)
		if err != nil {
			return err
		}

maybe this can be adapted and added as an example to the documention?

eloo avatar Aug 10 '20 07:08 eloo

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 07 '20 22:11 stale[bot]

I submitted PR #2572 which would fix this issue.

lyda avatar Jan 06 '24 09:01 lyda