How we can pass parameter in form of query string and access response in JSON in Servicestack - service

Below is service URL which return output in form of JSON.
http://localhost:8000/ByDept/ExmapleService?format=json
But I want to pass querystring parameter with this URL. Below is Service URL.
http://localhost:8000/ByDept/ExmapleService?abc=hello&format=json
here abc=hello is parameter which is pass through query string.
But using this url i am able to receive output in form of JSON.
So how we can pass parameter in form of query string and access response in json.?

The Content Negotiation section in the Routing docs shows different ways you can specify the response format, e.g:
/rockstars?format=json
/rockstars.json
In addition you can also specify a JSON response with the Accept Request Header, e.g:
Accept: application/json
Both of the above accept extra query params without changing the Response Type:
/rockstars?id=1&format=json
/rockstars.json?id=1
So I don't really understand what the question is.

Related

How can I put header parameter in a Url?

I want to get data from API. So first I want to get Json response from the browser. I Usually do it by specifying a query to pass the API key. But this time the API key is passed using header parameter instead of a query. How can I pass the API into a header parameter?
This is the base Url
"https://api-nba-v1.p.rapidapi.com"
This is the header parameter:
x-rapidapi-key
You can try to issue this request first using some rest client like postman or insomnia.

AWS Api Gateway Setting the header value to a default value using http Integration

I am using AWS API Gateway and I want to set my Integration type to http. I have the integrated url as https:// xxxxxx.com which takes a header "apikey". I am not expecting the end user to pass the header rather I want to set the apikey to some constant value.
I see that there is a way to force the user to make him pass the header(by making header required under the Method Request section. However, I want to set it to default.
For example in all the requests which are internally calling the URL inside the API gateway should pass the header value as "12345".
You can add/remove/override headers with an Integration Request Mapping Template.
In the API Gateway console, chose the relevant api/resourece/method. Go to Integration Request > Mapping Templates and chose your Content-Type (if requests are going to be received without a Content-Type header, set the Content-Type for the mapping template to application/json, which is the default behaviour).
Then in the actual mapping template add the following:
{
#set($context.requestOverride.header.apikey= "testMe")
}
This will add (or overwrite if it already exists) a header called apikey with the value "testMe" to all http requests downstream.
If you take this route, then you will need to also map over any other headers, path parameters, query parameters or body that you wish to pass through.
You could loop through the headers and query parameters like this.
## First set the header you are adding
#set($context.requestOverride.header.apikey= "testMe")
## Loop through all incoming headers and set them for downstream request
#foreach($param in $input.params().header.keySet())
#set($context.requestOverride.header[$param]= $input.params().header.get($param))
#if($foreach.hasNext) #end
#end
## Loop through all incoming query parameters and set them for downstream request
#foreach($param in $input.params().querystring.keySet())
#set($context.requestOverride.querystring[$param]= $input.params().querystring.get($param))
#if($foreach.hasNext) #end
#end
As you need to ensure that the header apikey is set to a default value, you should set the override for apikey before looping through the rest of the headers as only the first override will take effect.
The relevant AWS documentation can be found here.
The other alternative would be to point your API Gateway at a Lambda and make the call from the Lambda instead.
Firstly thanks to #KMO for his help. The following is the solution:-
Enable Http Proxy Integration.
Add the headers apikey=xxxx and Accept-Encoding=identity under the same Integration
Request -> Http Headers.
Under Settings -> Binary Media Types set the following as separate Binary Media Types
'*', */*. I mean as two different lines.This step is needed to resolve the Gzip action while returning the response.
Add the Query parameter country in the URL Query String Parameters section.
In the Integration Request map the country parameter to ctry by adding the value under mapped from as method.request.querystring.country. This will ensure that the query parameter country you passed in the main URL will be fed to the downstream url as parameter ctry.
The advantage of this apporoach is that, even if you override the header apikey, the one set under the Http Headers will take the precedence.

How do I send a JSON body as part of multipart/form-data with Paw?

I have a REST request that accepts multipart/form-data. This request is already working with an Angular front-end. It expects both an array of files in a parameter called files[] and a JSON body in a parameter with the name body. In Paw, it seems like I can get the multipart files to be recognized by my endpoint but the body doesn't appear to be the correct content type (application/json). Is there any way to specify the content type for each part of a multipart POST in Paw?
Details:
The error message I'm getting on the server-side is:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported
The config for the body in Paw is:

Unable to create mocks on post request with content-type : application/x-www-form-urlencoded

I am working with mountebank to create mocks for an External REST API
which is a POST request with content-type : application/x-www-form-urlencoded.
The API is of format
https://<url>/dpay/MPmt
and data payload is passed in format msg=01223~20170607114321~ABC~12345~NA~NA
I have to search on the basis of ~ABC~12345~ which would remain constant.
I have used contains, matches and equals predicates but was not able to run them while passing the payload in request body.
However, I was able to get it running when passing payload as a query parameter
https://url/dpay/MPmt?msg=01223~20170607114321~ABC~12345~NA~NA
but can't find a way to get it done when passed in request body.
Any pointers would be really appreciated.
Use the predicate "contains" and pass your matching string only. What I suspect is that you are using
"contains" :
{ "body" : {
"msg": "~ABC~12345~"
}
}
this will look for an msg variable in the request body, instead, use
"contains" : {"body": "~ABC~12345~"}
and it will match it directly in your request body.
I faced the same issue, Hope it works for you.

Apigility code-connected service - for POST method

I am a newbie to the apigility code-connected service & was able to create a RESTful service with fetch and fetchall class method on the mapper file.
Can someone point me a good sample for insert (POST) data via REST service ?
Thank you,
Kevin
POST is going to be used for creating a new resource typically. This means that in your request you're going to want the following headers:
Accept: application/json
Content-Type: application/json
The first tells Apigility what sort of a response it is expecting. The second says that the data you'll be providing to the API will be in json format.
Apigility uses json or json+hal by default for a return and expects json for the incoming data.
When you're creating a new resource, typically you'll be persisting it in a database and as such the id of the resource will be generated by your code or database. The rest of the resource will be provided by the caller to the API. Example:
POST /api/user
{
"username": "kevin voyce",
"firstname": "kevin",
"lastname":" "voyce"
}
If you do this, you should see a response of something like
405 - Method Not Allowed
The body of the error should indicate that the method has not been defined. The error message is coming from the create method in the resource. Inside this method, you'll see an argument called $data which at this point will consist of a PHP stdClass with fields matching the stuff you passed in via the JSON body.
This is where the fields part of configuring your API in Apigility comes in. If you set up the names of the fields and put validators on the fields, Apigility will make sure that the fields that are passed in conform to and are valid according to these validators before the call is made into your API. The same applies to not just POST, but PATCH and PUT as well. This means that within your methods you don't have to worry that the input hasn't been validated (as long as you correctly configured your validators).