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

POST requests return incorrect response code #63

Closed
phptek opened this issue May 27, 2018 · 4 comments
Closed

POST requests return incorrect response code #63

phptek opened this issue May 27, 2018 · 4 comments

Comments

@phptek
Copy link
Contributor

phptek commented May 27, 2018

If I issue an HTTP POST against an enabled datamodel-as-endpoint, and the resource does not exist, one is created and the module correctly scaffolds a 201 response. However, in postHandler() the logic attempts to address the spec for HTTP 201 responses and automatically redirect to the newly created resource at /api/v1/My-Data-Model/<ID>.json by the addition of a Location header.

This has at least 3 issues that I can see:

  1. If I wish to make exclusive use of canCreate() logic on my data-model, and not have canView() consulted at all, I cannot, other than to manually run removeHeader('Location') in my app's calling logic.
  2. I can no longer assert that a 201 "Created" code is returned by the response. The 'Location' header effectively renders the response a hard-out 200. (I appreciate that this is technically correct, but AFAICT is only one way to respect the spec)
  3. The data contained in the auto-returned response may be data/process heavy, something API consumers may not want for their system.

My suggestion is simple; to add an opt-out YML config option to skip the addition of the Location header, but where the default is to maintain current behaviour.

example:

        if ($this->config()->get('add_location_on_created')) {
            $urlSafeClassName = $this->sanitiseClassName(get_class($obj));
            $apiBase = $this->config()->api_base;
            $objHref = Director::absoluteURL($apiBase . "$urlSafeClassName/$obj->ID" . $type);
            $this->getResponse()->addHeader('Location', $objHref);
        }
@NightJar
Copy link
Contributor

Thanks for reporting @phptek - I'm going to link this to some other related issues from the past;
#10 and #62 sound very similar and may provide some historical context.

From interest, what is the version you have installed?

@phptek
Copy link
Contributor Author

phptek commented May 27, 2018

Currently running off dev-master

@robbieaverill
Copy link
Contributor

Fancy a pull request? 😄

@phptek
Copy link
Contributor Author

phptek commented May 30, 2018

@robbieaverill Done. See #64

phptek pushed a commit to phptek/silverstripe-restfulserver that referenced this issue May 30, 2018
phptek added a commit to phptek/silverstripe-restfulserver that referenced this issue Jun 5, 2018
phptek added a commit to phptek/silverstripe-restfulserver that referenced this issue May 17, 2019
phptek added a commit to phptek/silverstripe-restfulserver that referenced this issue May 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants