How to use additional save feature along with search feature HTTP API call
I have implemented an /GET HTTP endpoint to provide search feature. The user sends search terms in query parameters and receives JSON response containing all search results.
Now I have to add a new feature i.e. save search. It means the user sends same search parameters and can also send a boolean parameter say save=true. I have to save the search term in database in this case for future uses. However this parameter is not mandatory.
I am confused over the following points:
- Modify same GET HTTP endpoint allowing additional save parameter in query parameters.
- Modify same GET HTTP endpoint but passing save parameter in request body instead of query parameters as its backend state changing parameter.
- Use separate endpoint for save the parameters using POST method.
What is the standard/acceptable way of doing this?
rest http
add a comment |
I have implemented an /GET HTTP endpoint to provide search feature. The user sends search terms in query parameters and receives JSON response containing all search results.
Now I have to add a new feature i.e. save search. It means the user sends same search parameters and can also send a boolean parameter say save=true. I have to save the search term in database in this case for future uses. However this parameter is not mandatory.
I am confused over the following points:
- Modify same GET HTTP endpoint allowing additional save parameter in query parameters.
- Modify same GET HTTP endpoint but passing save parameter in request body instead of query parameters as its backend state changing parameter.
- Use separate endpoint for save the parameters using POST method.
What is the standard/acceptable way of doing this?
rest http
2. is impossible since GET should not have a body
– William Chong
Nov 22 at 11:27
@WilliamChong Its not impossible though yeah but GET should not have a request body as per HTTP standards and that why this situation for me.
– Shashwat Kumar
Nov 22 at 11:46
@RomanVottner updated the statements to make it more clear.
– Shashwat Kumar
Nov 22 at 12:05
add a comment |
I have implemented an /GET HTTP endpoint to provide search feature. The user sends search terms in query parameters and receives JSON response containing all search results.
Now I have to add a new feature i.e. save search. It means the user sends same search parameters and can also send a boolean parameter say save=true. I have to save the search term in database in this case for future uses. However this parameter is not mandatory.
I am confused over the following points:
- Modify same GET HTTP endpoint allowing additional save parameter in query parameters.
- Modify same GET HTTP endpoint but passing save parameter in request body instead of query parameters as its backend state changing parameter.
- Use separate endpoint for save the parameters using POST method.
What is the standard/acceptable way of doing this?
rest http
I have implemented an /GET HTTP endpoint to provide search feature. The user sends search terms in query parameters and receives JSON response containing all search results.
Now I have to add a new feature i.e. save search. It means the user sends same search parameters and can also send a boolean parameter say save=true. I have to save the search term in database in this case for future uses. However this parameter is not mandatory.
I am confused over the following points:
- Modify same GET HTTP endpoint allowing additional save parameter in query parameters.
- Modify same GET HTTP endpoint but passing save parameter in request body instead of query parameters as its backend state changing parameter.
- Use separate endpoint for save the parameters using POST method.
What is the standard/acceptable way of doing this?
rest http
rest http
edited Nov 22 at 12:05
asked Nov 22 at 11:19
Shashwat Kumar
3,43311541
3,43311541
2. is impossible since GET should not have a body
– William Chong
Nov 22 at 11:27
@WilliamChong Its not impossible though yeah but GET should not have a request body as per HTTP standards and that why this situation for me.
– Shashwat Kumar
Nov 22 at 11:46
@RomanVottner updated the statements to make it more clear.
– Shashwat Kumar
Nov 22 at 12:05
add a comment |
2. is impossible since GET should not have a body
– William Chong
Nov 22 at 11:27
@WilliamChong Its not impossible though yeah but GET should not have a request body as per HTTP standards and that why this situation for me.
– Shashwat Kumar
Nov 22 at 11:46
@RomanVottner updated the statements to make it more clear.
– Shashwat Kumar
Nov 22 at 12:05
2. is impossible since GET should not have a body
– William Chong
Nov 22 at 11:27
2. is impossible since GET should not have a body
– William Chong
Nov 22 at 11:27
@WilliamChong Its not impossible though yeah but GET should not have a request body as per HTTP standards and that why this situation for me.
– Shashwat Kumar
Nov 22 at 11:46
@WilliamChong Its not impossible though yeah but GET should not have a request body as per HTTP standards and that why this situation for me.
– Shashwat Kumar
Nov 22 at 11:46
@RomanVottner updated the statements to make it more clear.
– Shashwat Kumar
Nov 22 at 12:05
@RomanVottner updated the statements to make it more clear.
– Shashwat Kumar
Nov 22 at 12:05
add a comment |
1 Answer
1
active
oldest
votes
As far as I understood your question you try to store a search request and by storing it also retrieve the response in one go?
Usually GET
is used to retrieve a resources' state though as this method is defined as safe
it shouldn't be used if certain state is created for the invoked resource as persisting the search query would be. RFC 7231 further states that:
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
I therefore would refrain from option #1 or #2 as this might break interoperability by certain clients.
POST
on the otherhand is defined in RFC 7231 as
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.
It therefore should be used in every situation the other HTTP operations don't fit. The HTTP spec further defines that creating a new resource a 201 Created
HTTP status code should be returned including a HTTP response header named Location
containing the URI of the created resource. This URI can later be used to retrieve it's state (i.e. the performed search result).
From a client's perspective you are basically storing some query definition on the server and don't care where or how the server is actually persisting it. All you care is to retrieve a handle you can later on invoke. This doesn't prevent the server from returning the current search result within the response payload. And this is what I'd do exactly.
Proposed steps:
- Send search request via POST
- Store query definition
- Generate the URI for the stored query
- Perform the search according to the query
- Return a response with a
201 Created
status code andLocation
header pointing to the URI of the stored query and add the query result within the response payload
A client can later on use the returned URI to retrieve the current state of the resource, which the server can interpret as: execute the query stored for that URI and return the search result.
How the URI has to look like is not defined by the REST architecture. You might generate UUIDs or generate a hash value based on the query generate. The latter approach has the benefit that multiple identical queries wouldn't result in additional queries created but in the reusage of such. In such cases a redirect to the existing query resource should be performed to tell the client that his query already existed which also teaches the client the actual URI of the query resource as a side effect.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53429816%2fhow-to-use-additional-save-feature-along-with-search-feature-http-api-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As far as I understood your question you try to store a search request and by storing it also retrieve the response in one go?
Usually GET
is used to retrieve a resources' state though as this method is defined as safe
it shouldn't be used if certain state is created for the invoked resource as persisting the search query would be. RFC 7231 further states that:
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
I therefore would refrain from option #1 or #2 as this might break interoperability by certain clients.
POST
on the otherhand is defined in RFC 7231 as
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.
It therefore should be used in every situation the other HTTP operations don't fit. The HTTP spec further defines that creating a new resource a 201 Created
HTTP status code should be returned including a HTTP response header named Location
containing the URI of the created resource. This URI can later be used to retrieve it's state (i.e. the performed search result).
From a client's perspective you are basically storing some query definition on the server and don't care where or how the server is actually persisting it. All you care is to retrieve a handle you can later on invoke. This doesn't prevent the server from returning the current search result within the response payload. And this is what I'd do exactly.
Proposed steps:
- Send search request via POST
- Store query definition
- Generate the URI for the stored query
- Perform the search according to the query
- Return a response with a
201 Created
status code andLocation
header pointing to the URI of the stored query and add the query result within the response payload
A client can later on use the returned URI to retrieve the current state of the resource, which the server can interpret as: execute the query stored for that URI and return the search result.
How the URI has to look like is not defined by the REST architecture. You might generate UUIDs or generate a hash value based on the query generate. The latter approach has the benefit that multiple identical queries wouldn't result in additional queries created but in the reusage of such. In such cases a redirect to the existing query resource should be performed to tell the client that his query already existed which also teaches the client the actual URI of the query resource as a side effect.
add a comment |
As far as I understood your question you try to store a search request and by storing it also retrieve the response in one go?
Usually GET
is used to retrieve a resources' state though as this method is defined as safe
it shouldn't be used if certain state is created for the invoked resource as persisting the search query would be. RFC 7231 further states that:
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
I therefore would refrain from option #1 or #2 as this might break interoperability by certain clients.
POST
on the otherhand is defined in RFC 7231 as
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.
It therefore should be used in every situation the other HTTP operations don't fit. The HTTP spec further defines that creating a new resource a 201 Created
HTTP status code should be returned including a HTTP response header named Location
containing the URI of the created resource. This URI can later be used to retrieve it's state (i.e. the performed search result).
From a client's perspective you are basically storing some query definition on the server and don't care where or how the server is actually persisting it. All you care is to retrieve a handle you can later on invoke. This doesn't prevent the server from returning the current search result within the response payload. And this is what I'd do exactly.
Proposed steps:
- Send search request via POST
- Store query definition
- Generate the URI for the stored query
- Perform the search according to the query
- Return a response with a
201 Created
status code andLocation
header pointing to the URI of the stored query and add the query result within the response payload
A client can later on use the returned URI to retrieve the current state of the resource, which the server can interpret as: execute the query stored for that URI and return the search result.
How the URI has to look like is not defined by the REST architecture. You might generate UUIDs or generate a hash value based on the query generate. The latter approach has the benefit that multiple identical queries wouldn't result in additional queries created but in the reusage of such. In such cases a redirect to the existing query resource should be performed to tell the client that his query already existed which also teaches the client the actual URI of the query resource as a side effect.
add a comment |
As far as I understood your question you try to store a search request and by storing it also retrieve the response in one go?
Usually GET
is used to retrieve a resources' state though as this method is defined as safe
it shouldn't be used if certain state is created for the invoked resource as persisting the search query would be. RFC 7231 further states that:
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
I therefore would refrain from option #1 or #2 as this might break interoperability by certain clients.
POST
on the otherhand is defined in RFC 7231 as
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.
It therefore should be used in every situation the other HTTP operations don't fit. The HTTP spec further defines that creating a new resource a 201 Created
HTTP status code should be returned including a HTTP response header named Location
containing the URI of the created resource. This URI can later be used to retrieve it's state (i.e. the performed search result).
From a client's perspective you are basically storing some query definition on the server and don't care where or how the server is actually persisting it. All you care is to retrieve a handle you can later on invoke. This doesn't prevent the server from returning the current search result within the response payload. And this is what I'd do exactly.
Proposed steps:
- Send search request via POST
- Store query definition
- Generate the URI for the stored query
- Perform the search according to the query
- Return a response with a
201 Created
status code andLocation
header pointing to the URI of the stored query and add the query result within the response payload
A client can later on use the returned URI to retrieve the current state of the resource, which the server can interpret as: execute the query stored for that URI and return the search result.
How the URI has to look like is not defined by the REST architecture. You might generate UUIDs or generate a hash value based on the query generate. The latter approach has the benefit that multiple identical queries wouldn't result in additional queries created but in the reusage of such. In such cases a redirect to the existing query resource should be performed to tell the client that his query already existed which also teaches the client the actual URI of the query resource as a side effect.
As far as I understood your question you try to store a search request and by storing it also retrieve the response in one go?
Usually GET
is used to retrieve a resources' state though as this method is defined as safe
it shouldn't be used if certain state is created for the invoked resource as persisting the search query would be. RFC 7231 further states that:
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
I therefore would refrain from option #1 or #2 as this might break interoperability by certain clients.
POST
on the otherhand is defined in RFC 7231 as
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.
It therefore should be used in every situation the other HTTP operations don't fit. The HTTP spec further defines that creating a new resource a 201 Created
HTTP status code should be returned including a HTTP response header named Location
containing the URI of the created resource. This URI can later be used to retrieve it's state (i.e. the performed search result).
From a client's perspective you are basically storing some query definition on the server and don't care where or how the server is actually persisting it. All you care is to retrieve a handle you can later on invoke. This doesn't prevent the server from returning the current search result within the response payload. And this is what I'd do exactly.
Proposed steps:
- Send search request via POST
- Store query definition
- Generate the URI for the stored query
- Perform the search according to the query
- Return a response with a
201 Created
status code andLocation
header pointing to the URI of the stored query and add the query result within the response payload
A client can later on use the returned URI to retrieve the current state of the resource, which the server can interpret as: execute the query stored for that URI and return the search result.
How the URI has to look like is not defined by the REST architecture. You might generate UUIDs or generate a hash value based on the query generate. The latter approach has the benefit that multiple identical queries wouldn't result in additional queries created but in the reusage of such. In such cases a redirect to the existing query resource should be performed to tell the client that his query already existed which also teaches the client the actual URI of the query resource as a side effect.
edited Nov 22 at 12:34
answered Nov 22 at 12:28
Roman Vottner
5,68612438
5,68612438
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fstackoverflow.com%2fquestions%2f53429816%2fhow-to-use-additional-save-feature-along-with-search-feature-http-api-call%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
2. is impossible since GET should not have a body
– William Chong
Nov 22 at 11:27
@WilliamChong Its not impossible though yeah but GET should not have a request body as per HTTP standards and that why this situation for me.
– Shashwat Kumar
Nov 22 at 11:46
@RomanVottner updated the statements to make it more clear.
– Shashwat Kumar
Nov 22 at 12:05