Can wiremock json stubs be set to run my own script when end points are hit? - wiremock

I am using wiremock json stubs, and have been reading the doc at http://wiremock.org/docs/stubbing/ But I can't seem to locate a complete list of all the options one can put in the response part of json stub file.
I can see how to give a response, and even how to cycle through scenarios and the like. But can't seem to find a way to do something else when I hit a particular end point. Is there a way to specify one's own script to be run when a particular end point is hit? Preferably with the endpoint as an argument supplied to the script?
In my case, and I anticipate in others there is a good reason to have side effects when end points are hit.

Out of the box there isn't a way to call an external script. WireMock intended use is to provide a response to a request. There is a decent API and if your confident in java then it's not difficult to create custom Body transformer with the required logic.

Related

How server handle REST API path parameters

When design REST API, you can have
GET /userinfo?id=124,
you can also make the parameter part of the path:
GET /userinfo/124.
I understand on the server side, how the server get the parameter for the first case. But I do not understand how server can get the userid parameter in the second case: /userinfo/124.
Could anyone explain?
I understand on the server side, how the server get the parameter for the first case. But I do not understand how server can get the userid parameter in the second case: /userinfo/124.
Exactly the same way: the server parses the string.
Which is to say, in exactly the same way that we can take a string, and break it up into a path part and a query part, and then break the query part into a sequence of key and value pairs, we can also take the path part and break it up into a sequence of path segments.
It might help to look at RFC 6570, which describes URI templates, or to look at how the Java annotation javax.ws.rs.Path is used in routing, but under the covers it is all just string manipulation plus conventions for finding the right String to X parsing function.

How tcl rest package uses http::config

I'm using tcl rest package for executing rest API's, but i could not find any way to set the http::config options through this package. I'm using ::rest::create_interface proc for my rest call.
The http::config is global to the interpreter; just call it directly. You might need to call it once to get the current configuration before the call where you need a particular setting, again to set the configuration you need, and again afterwards to reset the configuration back to what it was.
# I'm assuming that it's just the Accept: header that you need to control;
# it's the only config item that you need to vary a lot when doing REST.
set oldmime [http::config -accept]
http::config -accept text/frobnicated
RestCallHere...
http::config -accept $oldmime
I think that this is really quite annoying (but can be made easier with Tcl 8.6's try…finally…), which is why I write my REST interfaces directly on top of the http package, but that's not something that is very reusable for you. If you do do that, remember that the -accept header can be switched back immediately after the http::geturl call, even if you are doing an asynchronous call, since it is used immediately when constructing the actual request headers.

How do I use multiple levels in my REST call?

I'm trying to create a REST service with the following signature for a GET call:
//somesite/api/customer/1/invoices
Of course using the correct path, I can get this to work, but all the documentation that I look at for REST tells me how to query .../api/customer or .../api/customer/id, but nothing tells me how to define and get to the level after id.
I suspect it will have something to do with the router code, but could use some instruction on how to get to that next level.
Thanks

Http DELETE with parameters using Jersey

I have code that contains several different DELETE methods, one of which takes a parameter, or at least ideally it would take a parameter. However when I make this request, either through CURL or through a web client, it doesn't work. The other DELETE requests function fine and I've hard-coded in the parameter that I want to pass just to see if the call works and it does. I've tried this with both PathParam and QueryParam and neither works. Here's how I'm using the PathParams, which I'm pretty sure is correct, QueryParams looks very similar so I don't want to post that too.
#DELETE
#Path("/byId/{id}")
public void deleteById(#PathParam("id") String id)
And then essentially the same thing for QueryParams but obviously the path is different
From what I understand a lot of RESTful APIs ignore any kind of request body with a DELETE request, or treat it as a PUT or POST. Is there any way around this? Basically I have a database that contains several objects and I need to delete one based on a unique identifier, to be passed in by the client. If there is no way around this is there some other way that I could do it?
Its possible that I'm missing something obvious here as I've only been using Jersey for a few weeks now and up to this point had never even heard of the concept of RESTful services.
You can send information to a RESTful service as either headers, path param, query param or message body.
If all the values go through as expected there is no problem with jax-rs/jersey. Now you need to debug the code and fix your implementation. jax-rs will only match a DELETE call with the DELETE http method you have implemented.
It is then your responsibility to actually perform a DELETE operation of the resource within the method. jax-rs or jersey is not going to complain if you do not DELETE or if you do some other action within the method.

How to describe Input Parameter in a RESTful Web Service

Im developeing a RESTful Service in which Processes can be executed and proivde a resulting calculation. For this i have modeled the process itself as a Resource (Example: /processes/translate). I want to execute the process by sending a GET request with appended Input Parameter as Query Parameter (Example: /processes/translate?input1=xxxx&input2=xxxxx).
Each process has different Input Parameter which are defined during the process creation in the backend. My Question is how should i document or describe which inputs are needed to execute a process in machine readable form. For Example in XML.
Until now ive integrated atom:link elements in the Representation. i thought that maybe including XFORM could be a soluttion?
Best Regards
Andre
I would not model this with a GET. While it's the easier solution, it's also (IMO) the least RESTful. I would have clients POST a document describing what they want you to translate and your service sends them back a URI where their answer can be found (some translations might take a while).
Example (ommiting a lot of HTTP headers/context)
POST /processes/translate
Content-Type: application/xml
...
<translation-request>
<input1 type="type1">....</input1>
<input2 type="type5">....</input2>
</translation-request>
Response:
200 OK
Content-Location: /processes/translate/jobs/1234
....
That's always an interesting question. We have a project called RESTx (http://restx.org), with which you can create RESTful web services very easily. You can write custom component code in either Java or Python and then create RESTful resources by sending parameter sets to the server, which are then stored. Each parameter set gets its own URI, though, so you can always just run the code with those parameters by accessing the new parameter set's URI.
Importantly, the entire RESTful API, is automatically created. RESTx examines the component code and then assembles the API description. We decided to describe parameters in a way that is human as well as machine readable. You can see examples of what that looks like in a browser or in plain JSON.
I'm the lead developer on that, so please feel free to contact me about any questions you might have.