Response Templating for a Static Json File - response

I am using WireMock in java to stub for a POST request. The request returns a json body file that is stored in my local. The stub looks like this:
wireMockServer.stubFor(get(urlMatching("/v1/invoices/.*"))
.willReturn(aResponse()
.withStatus(200)
.withBodyFile("testgetupgradeprorationamount/stubThree")));
Part of the response body file, "stubThree" looks like this:
"id": "ii_1EmM93Htp4Kkdrs8",
"object": "line_item",
"amount": 9600,
"currency": "usd",
"description": "Remaining time on 3 × Business after 17 Jun 2019",
"discountable": false,
"invoice_item": "ii_1EmM93HtpLyYzpmOC4Kkdrs8",
"livemode": false,
"metadata": {
},
"period": {
"end": 1563374954,
"start": 1560782957
}
The request url has a number of paramters and looks like this:
/v1/invoices/?subscription_items[0][quantity]=3&subscription_proration_date=1560892137&customer=cus_FHNIIE4b8LH7qL"
The stubbing works fine, but my goal is to give a dynamic response using response templating. I want to update the "start" field of the json file only, using the "subscription_proration_date" value from the request url.
I changed the start field and the stub like this:
"period": {
"end": 1566053354,
"start": "{{request.query.subscription_proration_date}}"
},
wireMockServer.stubFor(get(urlMatching("/v1/invoices/.*"))
.willReturn(aResponse()
.withStatus(200)
.withBodyFile("testgetupgradeprorationamount/stubThree")
.withTransformers("response-template")));
This is not working for me, so any directions would be really helpful. I also tried removing the quotations around the start field handlebar in the file, and that did not work either.
Thank you in advance!

so I was able to resolve my issue. The problem was that I did not add the proper extension to my WireMockServer instance:
.extensions(new ResponseTemplateTransformer(false));
If the boolean value is false, you additionally need to specify the transformer on a per stub basis like I did above. Otherwise, the extension is applied to all stubs.

Related

Using an API with Zapier but getting "The app returned ""rest_data" param is incorrect. JSON data expected"."

So i grab data from an Airtable and then have a custom PUT as the second action using Samba Lives API, specifically Sessions -> PUT Session https://documenter.getpostman.com/view/2014682/samba-live-rest-api/6YzutHM#c87c1db7-2b5c-9b2b-7d63-6b531793cfe2
This is the data in the custom PUT:
Method: PUT
URL: https://samba.live/api/2/ourusername/session
Data Pass-Through? No
Data:
input_type=json&rest_data={
"topic": "Crazy Topic",
"duration": 30,
"start_time": "2025-09-10 12:00:00",
"invited_participants":[
{
"email":"flast#company.com",
"first_name":"First",
"last_name":"Last",
"role":2,
"send_email_invitation":true
},
{
"email": "first.last#gmail.com",
"first_name": "First",
"last_name": "Last",
"role": 1,
"send_email_invitation": true
}
],
}
Unflatten- yes
Basic Auth- ourusername|ourpassword
Headers- none
But the test fails and says "App returned "rest_data" param is incorrect. JSON is expected."
I'm not really sure what to try and Zapier just said we don't help with that. Only thing i could think to try was to delete the input_type section, and that returned with an error saying "Topic is required." Same thing when i left most of it and just deleted the rest_data part.
Any ideas?
Fixed - apparently the comma after the ] threw everything off.

Can jenkins extended choice parameter be made dependent on another parameter's value?

I am using extended choice parameter with JSON parameter type in my declarative Jenkins pipeline. I have found it very good for providing custom UI for parameters and it returns a json based on user inputs.
I have a use case where what options are shown to user depends upon another parameter's value. I can achieve such functionality with active choice parameter but then I am stuck with radio buttons, checkbox, html input etc.
I found a suitable option here where I can make a property inside json dependent on another property:
{
"title": "An object",
"type": "object",
"properties": {
"fieldOne": {
"title": "I should be changed to 'foo'",
"type": "string",
"enum": ["foo","bar"],
"default": "bar"
},
"depender1": {
"title": "I depend on fieldOne to be 'foo'",
"type": "string",
"enum": ["lorem","ipsum"],
"options": {
"dependencies": {
"fieldOne": "foo"
}
}
},
"depender2": {
"title": "I depend on fieldOne to be 'bar'",
"type": "string",
"enum": ["dolor", "sit"],
"options": {
"dependencies": {
"fieldOne": "bar"
}
}
}
}
}
This works great when I try it here
But when I try the same on jenkins, it doesn't work. It shows all 3 textboxes. I saw the option of watching other params too but I couldn't find how to use it as an if else for my parameter.
This is a simple example, what I want to achieve requires UI of a dropdown-1 + Array(dropdown-2 + text field+text-field) where in array's text-field depend on value of dropdown-1, I cannot create the same UI in active choice.
Does any one know how options.dependencies could work in jenkins or same could be achieved using watch/other plugins?
if i got your question right, so you want to make it more smart way to select parameters.
So you can do it via groovy script.
Here is my example you can see on pic:
freestyle job config
Sorry, but i don't know how to better show freestyle job config.
So, logic is quite simple:
i'm collecting JSON on first parameter, and doing some parsing for it.
and then im using Environmets variable to show it's contents, depending on result from first part.
ps. i'm struggling right now with variable Hosts, as i don't know how to pass it in final steps without asking user for input.
But i believe you got the idea how you can do it.

Two identical metadata requests, first one returns status 200, the 2nd one 404

Below you will find 2 metadata requests to the same OData Service. Except the cookie digit stream, they look like 2 drops of water, completely identical.
First one is triggered via the manifest.json file and its result is successful. Copied the data source definition to the manifest.json file of a second different application and put at work the debugger, expecting the same successful result.
"dataSources": { "mainService": { "uri": "/Uni_Sandpit_Virtual/sap/opu/odata/SAP/ZCONTRACTS_SRV/", "type": "OData", "settings": { "odataVersion": "2.0" } } },
To my absolute surprise the second metadata call returns 404 (not found). What do I miss here?
Best regards,
Greg
Request returning status 200
Request returning status 404
​Found the issue, posting it in case some other runs on the same problem.
Missing "path" block inside the neo-app.json file:
{ "path": "/Uni_Sandpit_Virtual", "target": { "type": "destination", "name": "Uni_Sandpit_Virtual" },

Handling better response from Aws::Route53::Client.new

First time trying to use the Ruby AWS ADK V2 and I am trying to format the data i am getting back and it seems quiet hard getting it into useable format.
All I want to do is get a list of Hosted Zones and display in a table.
I have a helper that has:
def hosted_zones
r53 = Aws::Route53::Client.new
#convert to hash first so we can parse and covert to json
h = (r53.list_hosted_zones).to_hash
j = JSON.parse((h.to_json))
end
which then returns me the following JSON:
{
"hosted_zones": [{
"id": "/hostedzone/Z1HSDGASSSME",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:45.277646707+01:00",
"config": {
"comment": "Private DNS zone for stage",
"private_zone": true
},
"resource_record_set_count": 10
}, {
"id": "/hostedzone/ZJDGASSS0ZN3",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:41.290143511+01:00",
"config": {
"comment": "Public DNS zone for stage",
"private_zone": false
},
"resource_record_set_count": 7
}],
"is_truncated": false,
"max_items": 100
}
To which I am running a really but while statement to interact through all the hosted_zone entries into a table.
Is this the best way to get the response or can you request the response to be json already?
Why are you converting a hash to JSON, only to convert it to a hash again? JSON.parse(some_hash.to_json) will just give you some_hash.
That being said, I don't think it is possible to get JSON directly from AWS, mainly due to the fact that their API responds with XML. I think that your solution is ideal if that's all you're doing, but if you want, you can make a request with an HTTP client and then take the XML that you receive and use something like ActiveSupport's Hash.from_xml to create a hash that you can then convert to JSON.

Obj. C - how to deal with wrong written JSON

I obtained some badly written JSON file that's totally not readable through any AFNetworking and/or any other JSON serializing library. I must underline that I am not able to force to change it on server-side so I have to parse it as is.
Unfortunately it has some minor errors (I will paste small part of it):
locations = [{
"city": "Tokio",
(...)
"link": "http://somethig.com",
"text": "Mon-Fr.",
}, {
(... same repeated mistakes, but they're not regular)
}]
Aaand to parse it correctly in XCode i need to change it to correct format e.g.:
{
"locations": [{
"city": "Tokio",
(...)
"link": "http://somethig.com",
"text": "Mon-Fr."
}, {
(...)
}]
}
Do you have any idea how to deal with that?
If I will have to write my own parser - please advice me how to. Any help will be appreciated. I download this JSON from http link.
It looks like the API is sending you a string of JavaScript code. In JavaScript, you would use JSON.stringify() to convert the object to valid JSON. Here's an example from an interactive node.js shell:
> locations = [{
... "city": "Tokio",
... "link": "http://somethig.com",
... "text": "Mon-Fr.",
... }]
[ { city: 'Tokio', link: 'http://somethig.com', text: 'Mon-Fr.' } ]
> JSON.stringify(locations)
'[{"city":"Tokio","link":"http://somethig.com","text":"Mon-Fr."}]'
If you really can't change this on the server side, you might try creating a hidden UIWebView, adding the JavaScript, calling JSON.stringify(), and extracting the result. However, this will use many more resources than are needed. It would be much better (in terms of computing power, memory, and your time) to have the API call JSON.stringify() and send you a valid JSON string.
You could also try porting some of json3.js to Objective-C or Swift. I would start with the section beginning // Public: `JSON.stringify`. if you attempt this.

Resources