How does one get all query params from within a endpoint? - scala

I'm writing a toy finch project and trying to allow requests to specify arbitrary query parameters to set some value. I am able to retrieve the value for a known parameter name (i.e. param[String]("foo")) but how can I get a Map[String,String] of all the query parameters?
i.e. hit /myendpoint?foo=bar&baz=baq and within the response handler be able to access the value Map("foo"->"bar", "baz"->"baq")
I looked in the docs and there seems to be some way to access all the headers but no equivalent functionality is given for accessing params.

Related

Mapping multiple values for x-amazon-apigateway-integration.requestParameters for a single key?

I'm trying to create a new API on Amazon API gateway but for my use-case I want to concatenate 2 values for a single key in request parameters. How can we specify multiple values for a single key in integration.requestParameters?
This is what I'm trying to achieve but the syntax is not correct and it's giving me error:
For one to one mapping we use something like this
integration.request.querystring.start: "method.request.querystring.oauth_code"
Documentation link
I was able to solve it by following instructions in this Stackoverflow Answer.
Basically you need to add multiple parameters in the endpoint URL itself in the integration request, make sure that you are accepting those parameters as URL Path Parameters in the Integration Request.

Routing incoming request

I am trying to create a simple API using Go that performs certain operations depending on the data provided.
I was planning to provide JSON data to this API and get details from it for further use.
Since I was trying to provide JSON data I created the routing using gorilla/mux as below:
router.HandleFunc("/msa/dom/perform-factory-reset?json={jsonData}", CallGet)
log.Fatal(http.ListenAndServe(":8080", router))
But while trying to hit the endpoint http://localhost:8080/msa/dom/perform-factory-reset?json={"vrf":"ds","ip":"45","mac":"452","method":"gfd"} I am getting 404 page not found error.
Hence I tried to change the implementation such that new routing is done as:
router.HandleFunc("/msa/dom/perform-factory-reset/json={jsonData}", CallGet)
This works absolutely fine and I am able to perform desired tasks. Could someone tell me why this is happening?
Is the router gorilla/mux? If so, you cannot add query parameters to path like that. You have to:
router.Path("/msa/dom/perform-factory-reset").
Queries("json","{jsonData}").HandlerFunc(CallGet)
If it is some other router, then you still probably have to register path without the query parameters, and then get the query parameter values in the handler from the request.

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?
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 and Location 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.

Include multiple query parameters in HCM cloud rest Get call

I have an hcm cloud instance. I'm working on the rest api which are provided by the cloud.
I want to get an employee by matching both PersonNumber as well as DateOfBirth.
But whatever I tried based on the first parameter, I'm getting the output. Second is not even checked.
Can anyone help?
This is the rest url I'm using
https://host:port/hcmCoreApi/resources/11.12.1.0/emps/?q=DateOfBirth=1991-09-19&PersonNumber=240
For passing multiple search items in a query parameter in a rest call, the structure should be as following
https://host:port/hcmCoreApi/resources/11.12.1.0/emps/?q=DateOfBirth='1991-09-19'&PersonNumber=240
Basically quotes '' are required around String inputs and integer can be passed directly.

LoopBack: How do I use PUT to replace an entry

I struggle a bit with a basic PUT in a LoopBack project, but I cannot find any information about my problem. If use PUT to add an entry everything works fine, I just pass my data and there is a new entry in the database. Now I want to replace an entry, as described in the documentation for PUT (I am using the API explorer for my tests). My idea was to pass an 'id' (in addition to the changed data) and LoopBack would replace the corresponding entry. But that does not work, I get the error:
The `Model` instance is not valid. Details: `id` can't be set
How can I use PUT to replace an entry?
The id property in the PUT payload should be optional. If you're using the standard Loopback model CRUD endpoints that are set up during model generation you have two options:
Using the PUT /ModelName/{id} option where LB will find the model instance based on the query parameter passed into the URL.
Using the POST /ModelName/{id}/replace endpoint. This endpoint will replace all attributes of a given model instance based on the id that's passed into the URL.
Passing in the id property in the payload of the request is optional for both endpoints. Loopback will find the model instance based on the id param of the URL.