Return a 304 HTTP response from within a REST API method?How to set HTTP Response Code in Custom REST APIHow to integrate magento REST API with third party?How to return a response from _create api rest methodNo response from POST to custom REST Api routeExtending REST API, route to custom methodHow to set HTTP Response Code in Custom REST APIMagento 2: Sending a Custom Header/Response from a ControllerREST API Update Product HTTP Method PUT or POST?Magento REST API Response is not return from _create() methodReturn Shipping Methods in Store REST APICustom REST API returns duplicate “response” tag
Export economy of Mars
Gold Battle KoTH
What is Modern Vipassana?
What printing process is this?
Need help with modelling a cylinder with a piece cut out?
Is law enforcement responsible for damages made by a search warrant?
Is it moral to remove/hide certain parts of a photo, as a photographer?
Why is the Vasa Museum in Stockholm so Popular?
Feedback diagram
Is it uncompelling to continue the story with lower stakes?
Basic CPA walkthrough
Is there a general term for the items in a directory?
How to transform a function from f[#1] to f[x]
Can an unintentional murderer leave Ir Miklat for Shalosh Regalim?
In a KP-K endgame, if the enemy king is in front of the pawn, is it always a draw?
Why does the friction act on the inward direction when a car makes a turn on a level road?
Why wasn't interlaced CRT scanning done back and forth?
How can I perform a deterministic physics simulation?
Does a bard know when a character uses their Bardic Inspiration?
A wiild aanimal, a cardinal direction, or a place by the water
How to call made-up data?
Is an "are" omitted in this sentence
Astable 555 circuit not oscillating
Who's behind community AMIs on Amazon EC2?
Return a 304 HTTP response from within a REST API method?
How to set HTTP Response Code in Custom REST APIHow to integrate magento REST API with third party?How to return a response from _create api rest methodNo response from POST to custom REST Api routeExtending REST API, route to custom methodHow to set HTTP Response Code in Custom REST APIMagento 2: Sending a Custom Header/Response from a ControllerREST API Update Product HTTP Method PUT or POST?Magento REST API Response is not return from _create() methodReturn Shipping Methods in Store REST APICustom REST API returns duplicate “response” tag
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In a custom REST API endpoint method, returning the object promised in the service class interface will generate a 200 OK response. Additionally, I can return a specific HTTP error code by throwing an particular type of exception. For example, throwing a NoSuchEntityException returns a 404 NOT FOUND and a AuthorizationException returns a 401 UNAUTHORIZED.
My question: is there a way to return other HTTP codes, such as a 304 NOT MODIFIED code?
magento2 api rest
add a comment |
In a custom REST API endpoint method, returning the object promised in the service class interface will generate a 200 OK response. Additionally, I can return a specific HTTP error code by throwing an particular type of exception. For example, throwing a NoSuchEntityException returns a 404 NOT FOUND and a AuthorizationException returns a 401 UNAUTHORIZED.
My question: is there a way to return other HTTP codes, such as a 304 NOT MODIFIED code?
magento2 api rest
add a comment |
In a custom REST API endpoint method, returning the object promised in the service class interface will generate a 200 OK response. Additionally, I can return a specific HTTP error code by throwing an particular type of exception. For example, throwing a NoSuchEntityException returns a 404 NOT FOUND and a AuthorizationException returns a 401 UNAUTHORIZED.
My question: is there a way to return other HTTP codes, such as a 304 NOT MODIFIED code?
magento2 api rest
In a custom REST API endpoint method, returning the object promised in the service class interface will generate a 200 OK response. Additionally, I can return a specific HTTP error code by throwing an particular type of exception. For example, throwing a NoSuchEntityException returns a 404 NOT FOUND and a AuthorizationException returns a 401 UNAUTHORIZED.
My question: is there a way to return other HTTP codes, such as a 304 NOT MODIFIED code?
magento2 api rest
magento2 api rest
asked Mar 31 '16 at 20:16
brendanmckeownbrendanmckeown
806 bronze badges
806 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Custom status codes are currently not supported, you can only get one of the following: 200, 400, 401, 403, 404, 406, 500 (see MagentoFrameworkWebapiException). If you have real use case when you need this functionality, feel free to create pull request or file an issue at https://github.com/magento/magento2 .
Hi @Alex Paliarush, Thanks for the answer. Just I want to know how we can send the above-mentioned error code from our custom rest api call(Post/Get)? Can you please show me the example?
– Manish
Mar 28 '17 at 5:46
Hi using this magento.stackexchange.com/questions/83852/…
– Manish
Mar 28 '17 at 6:21
add a comment |
You can try send a custom http code in your service class using ResponseInterface.
Here a little example:
- Define your service interface like this:
<?php
namespace TestModuleApi;
interface TestApiInterface
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo();
Now on your service class definition
<?php
namespace PaymentezModuleModel;
use TestModuleApiTestApiInterface;
use MagentoFrameworkApp
RequestInterface,
ResponseInterface
;
class TestApi implements TestApiInterface
/**
* @var MagentoFrameworkAppRequestInterface
*/
protected $request;
/**
* @var MagentoFrameworkAppResponseInterface
*/
protected $response;
/**
* CustomerAddress constructor.
* @param MagentoFrameworkAppRequestInterface $request
* @param MagentoFrameworkAppResponseInterface $response
*/
public function __construct(RequestInterface $request,
ResponseInterface $response)
$this->request = $request;
$this->response = $response;
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo()
$rawContent = $this->request->getContent();
$params = json_decode($rawContent, true);
$response = $this->response;
$response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
$response->setContent(json_encode([
'status' => false,
'request_params' => $params
]));
$response->send();
// Ugly trick 😢 for force this response
die;
Now you can see the use of die sentence, but this trick prevent the next override of http status code and headers on the finally response
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f108871%2freturn-a-304-http-response-from-within-a-rest-api-method%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Custom status codes are currently not supported, you can only get one of the following: 200, 400, 401, 403, 404, 406, 500 (see MagentoFrameworkWebapiException). If you have real use case when you need this functionality, feel free to create pull request or file an issue at https://github.com/magento/magento2 .
Hi @Alex Paliarush, Thanks for the answer. Just I want to know how we can send the above-mentioned error code from our custom rest api call(Post/Get)? Can you please show me the example?
– Manish
Mar 28 '17 at 5:46
Hi using this magento.stackexchange.com/questions/83852/…
– Manish
Mar 28 '17 at 6:21
add a comment |
Custom status codes are currently not supported, you can only get one of the following: 200, 400, 401, 403, 404, 406, 500 (see MagentoFrameworkWebapiException). If you have real use case when you need this functionality, feel free to create pull request or file an issue at https://github.com/magento/magento2 .
Hi @Alex Paliarush, Thanks for the answer. Just I want to know how we can send the above-mentioned error code from our custom rest api call(Post/Get)? Can you please show me the example?
– Manish
Mar 28 '17 at 5:46
Hi using this magento.stackexchange.com/questions/83852/…
– Manish
Mar 28 '17 at 6:21
add a comment |
Custom status codes are currently not supported, you can only get one of the following: 200, 400, 401, 403, 404, 406, 500 (see MagentoFrameworkWebapiException). If you have real use case when you need this functionality, feel free to create pull request or file an issue at https://github.com/magento/magento2 .
Custom status codes are currently not supported, you can only get one of the following: 200, 400, 401, 403, 404, 406, 500 (see MagentoFrameworkWebapiException). If you have real use case when you need this functionality, feel free to create pull request or file an issue at https://github.com/magento/magento2 .
answered Mar 31 '16 at 22:43
Alex PaliarushAlex Paliarush
11.3k2 gold badges38 silver badges51 bronze badges
11.3k2 gold badges38 silver badges51 bronze badges
Hi @Alex Paliarush, Thanks for the answer. Just I want to know how we can send the above-mentioned error code from our custom rest api call(Post/Get)? Can you please show me the example?
– Manish
Mar 28 '17 at 5:46
Hi using this magento.stackexchange.com/questions/83852/…
– Manish
Mar 28 '17 at 6:21
add a comment |
Hi @Alex Paliarush, Thanks for the answer. Just I want to know how we can send the above-mentioned error code from our custom rest api call(Post/Get)? Can you please show me the example?
– Manish
Mar 28 '17 at 5:46
Hi using this magento.stackexchange.com/questions/83852/…
– Manish
Mar 28 '17 at 6:21
Hi @Alex Paliarush, Thanks for the answer. Just I want to know how we can send the above-mentioned error code from our custom rest api call(Post/Get)? Can you please show me the example?
– Manish
Mar 28 '17 at 5:46
Hi @Alex Paliarush, Thanks for the answer. Just I want to know how we can send the above-mentioned error code from our custom rest api call(Post/Get)? Can you please show me the example?
– Manish
Mar 28 '17 at 5:46
Hi using this magento.stackexchange.com/questions/83852/…
– Manish
Mar 28 '17 at 6:21
Hi using this magento.stackexchange.com/questions/83852/…
– Manish
Mar 28 '17 at 6:21
add a comment |
You can try send a custom http code in your service class using ResponseInterface.
Here a little example:
- Define your service interface like this:
<?php
namespace TestModuleApi;
interface TestApiInterface
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo();
Now on your service class definition
<?php
namespace PaymentezModuleModel;
use TestModuleApiTestApiInterface;
use MagentoFrameworkApp
RequestInterface,
ResponseInterface
;
class TestApi implements TestApiInterface
/**
* @var MagentoFrameworkAppRequestInterface
*/
protected $request;
/**
* @var MagentoFrameworkAppResponseInterface
*/
protected $response;
/**
* CustomerAddress constructor.
* @param MagentoFrameworkAppRequestInterface $request
* @param MagentoFrameworkAppResponseInterface $response
*/
public function __construct(RequestInterface $request,
ResponseInterface $response)
$this->request = $request;
$this->response = $response;
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo()
$rawContent = $this->request->getContent();
$params = json_decode($rawContent, true);
$response = $this->response;
$response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
$response->setContent(json_encode([
'status' => false,
'request_params' => $params
]));
$response->send();
// Ugly trick 😢 for force this response
die;
Now you can see the use of die sentence, but this trick prevent the next override of http status code and headers on the finally response
add a comment |
You can try send a custom http code in your service class using ResponseInterface.
Here a little example:
- Define your service interface like this:
<?php
namespace TestModuleApi;
interface TestApiInterface
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo();
Now on your service class definition
<?php
namespace PaymentezModuleModel;
use TestModuleApiTestApiInterface;
use MagentoFrameworkApp
RequestInterface,
ResponseInterface
;
class TestApi implements TestApiInterface
/**
* @var MagentoFrameworkAppRequestInterface
*/
protected $request;
/**
* @var MagentoFrameworkAppResponseInterface
*/
protected $response;
/**
* CustomerAddress constructor.
* @param MagentoFrameworkAppRequestInterface $request
* @param MagentoFrameworkAppResponseInterface $response
*/
public function __construct(RequestInterface $request,
ResponseInterface $response)
$this->request = $request;
$this->response = $response;
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo()
$rawContent = $this->request->getContent();
$params = json_decode($rawContent, true);
$response = $this->response;
$response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
$response->setContent(json_encode([
'status' => false,
'request_params' => $params
]));
$response->send();
// Ugly trick 😢 for force this response
die;
Now you can see the use of die sentence, but this trick prevent the next override of http status code and headers on the finally response
add a comment |
You can try send a custom http code in your service class using ResponseInterface.
Here a little example:
- Define your service interface like this:
<?php
namespace TestModuleApi;
interface TestApiInterface
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo();
Now on your service class definition
<?php
namespace PaymentezModuleModel;
use TestModuleApiTestApiInterface;
use MagentoFrameworkApp
RequestInterface,
ResponseInterface
;
class TestApi implements TestApiInterface
/**
* @var MagentoFrameworkAppRequestInterface
*/
protected $request;
/**
* @var MagentoFrameworkAppResponseInterface
*/
protected $response;
/**
* CustomerAddress constructor.
* @param MagentoFrameworkAppRequestInterface $request
* @param MagentoFrameworkAppResponseInterface $response
*/
public function __construct(RequestInterface $request,
ResponseInterface $response)
$this->request = $request;
$this->response = $response;
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo()
$rawContent = $this->request->getContent();
$params = json_decode($rawContent, true);
$response = $this->response;
$response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
$response->setContent(json_encode([
'status' => false,
'request_params' => $params
]));
$response->send();
// Ugly trick 😢 for force this response
die;
Now you can see the use of die sentence, but this trick prevent the next override of http status code and headers on the finally response
You can try send a custom http code in your service class using ResponseInterface.
Here a little example:
- Define your service interface like this:
<?php
namespace TestModuleApi;
interface TestApiInterface
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo();
Now on your service class definition
<?php
namespace PaymentezModuleModel;
use TestModuleApiTestApiInterface;
use MagentoFrameworkApp
RequestInterface,
ResponseInterface
;
class TestApi implements TestApiInterface
/**
* @var MagentoFrameworkAppRequestInterface
*/
protected $request;
/**
* @var MagentoFrameworkAppResponseInterface
*/
protected $response;
/**
* CustomerAddress constructor.
* @param MagentoFrameworkAppRequestInterface $request
* @param MagentoFrameworkAppResponseInterface $response
*/
public function __construct(RequestInterface $request,
ResponseInterface $response)
$this->request = $request;
$this->response = $response;
/**
* @api
*
* @return MagentoFrameworkAppResponseInterface
*/
public function foo()
$rawContent = $this->request->getContent();
$params = json_decode($rawContent, true);
$response = $this->response;
$response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
$response->setContent(json_encode([
'status' => false,
'request_params' => $params
]));
$response->send();
// Ugly trick 😢 for force this response
die;
Now you can see the use of die sentence, but this trick prevent the next override of http status code and headers on the finally response
answered Jul 24 at 22:05
SairokoSairoko
111 bronze badge
111 bronze badge
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f108871%2freturn-a-304-http-response-from-within-a-rest-api-method%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown