Skip to content
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

Fixed Issue x-www-form-urlencoded #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 49 additions & 35 deletions rest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,40 @@ func (h requestHandler) handleCreate(handler ResourceHandler) http.Handler {
version := ctx.Version()
rules := handler.Rules()

data, err := decodePayload(payloadString(r.Body))
if err != nil {
// Payload decoding failed.
ctx = ctx.setError(BadRequest(err.Error()))
if r.Header["Content-Type"][0] == "application/x-www-form-urlencoded" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be:

if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {

var data Payload
resource, _ := handler.CreateResource(ctx, data, ctx.Version())
resource = applyOutboundRules(resource, rules, version)
ctx = ctx.setResult(resource)
ctx = ctx.setStatus(http.StatusCreated)
} else {
data, err := applyInboundRules(data, rules, version)
data, err := decodePayload(payloadString(r.Body))
if err != nil {
// Type coercion failed.
ctx = ctx.setError(UnprocessableRequest(err.Error()))
ctx = ctx.setError(BadRequest(err.Error()))
} else {
resource, err := handler.CreateResource(ctx, data, ctx.Version())
if err == nil {
resource = applyOutboundRules(resource, rules, version)
}

if resource != nil {
ctx = ctx.setResult(resource)
ctx = ctx.setStatus(http.StatusCreated)
data, err := applyInboundRules(data, rules, version)
if err != nil {
// Type coercion failed.
ctx = ctx.setError(UnprocessableRequest(err.Error()))
} else {
ctx = ctx.setStatus(http.StatusNoContent)
}
resource, err := handler.CreateResource(ctx, data, ctx.Version())
if err == nil {
resource = applyOutboundRules(resource, rules, version)
}

if err != nil {
ctx = ctx.setError(err)
if resource != nil {
ctx = ctx.setResult(resource)
ctx = ctx.setStatus(http.StatusCreated)
} else {
ctx = ctx.setStatus(http.StatusNoContent)
}

if err != nil {
ctx = ctx.setError(err)
}
}
}
}

h.sendResponse(w, ctx)
})
}
Expand Down Expand Up @@ -276,25 +282,33 @@ func (h requestHandler) handleUpdate(handler ResourceHandler) http.Handler {
version := ctx.Version()
rules := handler.Rules()

data, err := decodePayload(payloadString(r.Body))
if err != nil {
// Payload decoding failed.
ctx = ctx.setError(BadRequest(err.Error()))
if r.Header["Content-Type"][0] == "application/x-www-form-urlencoded" {
var data Payload
resource, _ := handler.CreateResource(ctx, data, ctx.Version())
resource = applyOutboundRules(resource, rules, version)
ctx = ctx.setResult(resource)
ctx = ctx.setStatus(http.StatusCreated)
} else {
data, err := applyInboundRules(data, rules, version)
data, err := decodePayload(payloadString(r.Body))
if err != nil {
// Type coercion failed.
ctx = ctx.setError(UnprocessableRequest(err.Error()))
// Payload decoding failed.
ctx = ctx.setError(BadRequest(err.Error()))
} else {
resource, err := handler.UpdateResource(
ctx, ctx.ResourceID(), data, version)
if err == nil {
resource = applyOutboundRules(resource, rules, version)
}
data, err := applyInboundRules(data, rules, version)
if err != nil {
// Type coercion failed.
ctx = ctx.setError(UnprocessableRequest(err.Error()))
} else {
resource, err := handler.UpdateResource(
ctx, ctx.ResourceID(), data, version)
if err == nil {
resource = applyOutboundRules(resource, rules, version)
}

ctx = ctx.setResult(resource)
ctx = ctx.setError(err)
ctx = ctx.setStatus(http.StatusOK)
ctx = ctx.setResult(resource)
ctx = ctx.setError(err)
ctx = ctx.setStatus(http.StatusOK)
}
}
}

Expand Down