Use JSON schema check on POST request with WireMock - json

I'm using WireMock (Docker) docker.io/rodolpheche/wiremock:2.27.2-alpine. I set it up via Docker Compose and everything works flawlessly.
I'm trying to set up a stub/mapping to check the JSON input against the corresponding JSON schema. Basically, this is the stub/mapping:
{
"name": "Create Character Stub",
"request": {
"headers": {
"accept": {
"caseInsensitive": true,
"equalTo": "application/json"
},
"content-type": {
"caseInsensitive": true,
"equalTo": "application/json"
}
},
"method": "POST",
"url": "/api/characters",
"body": {
// [start] I made this up, but that's what I'm trying to achieve
"type": "JSON_SCHEMA",
"location": ""schemas/create-character.json""
// [end]
}
},
"response": {
"headers": {
"location": "{{ request.path }}/{{ randomValue type='UUID' }}"
},
"status": 201,
"transformers": ["response-template"]
}
}
...and this is (an excerpt of) the JSON schema:
{
"$schema": "http://json-schema.org/draft/2019-09/schema#",
"type": "object",
"description": "Character entity",
"properties": {
"first_name": {
"type": "string",
"description": ""
},
"last_name": {
"type": "string",
"description": ""
},
"age": {
"type": "number",
"description": ""
}
},
"additionalProperties": false,
"required": ["first_name", "last_name", "age"]
}
Is there a way to accomplish this with WireMock? I've been searching for something like that for a while. The closer I've found is to use JSON path, but that's not quite the same as checking for a proper schema — and could be tedious task with long JSON requests.
I searched through the issues in the WireMock's GitHub repository, but I can only see this one (closed by the way). Still, I don't see a way to do exactly what I'm looking for. I just have as input a JSON schema.

Related

Using Azure Logic App to Insert Response from SurveyGizmo via HTTP Post (JSON)

SurveyGizmo provides a "webhook" that posts data to a given URL as JSON or a simple post. I need to insert this data into a SQL Server database in Azure. I am able to get the trigger (the post) to initiate the app but am having difficulty extracting the data out of the JSON submitted.
I used webhook.site to get a sample of the JSON sent via SurveyGizmo. I used this to paste into the "sample payload" for the schema under the trigger (HTTP Request). I see the data, the field names, etc. in the sample but I can't seem to get values out of it in my second step which is the insert record into SQL Server. I'll paste the sample JSON below. What I see when I select a "parameter" under the SQL part is many, many "answer" choices under dynamic content. I see that is repeated many times in the below sample so I'm thinking it's just not reading the schema correctly or I need to write out the schema. Any thoughts?
{
"is_test_data": null,
"session_id": "1567451759_5d6d6a6f705997.66294551",
"language": "English",
"date_started": "2019-09-02 15:15:59 EDT",
"link_id": null,
"url_variables": [
],
"ip_address": "99.85.42.146",
"referer": "https:\/\/app.surveygizmo.com\/builder\/test\/id\/5147523",
"user_agent": "Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/70.0.3538.102 Safari\/537.36 Edge\/18.18362",
"response_time": null,
"data_quality": [
],
"survey_data": {
"3": {
"id": 3,
"type": "TEXTBOX",
"question": "Please enter your email address.\u00a0 Doing so will allow you to retrieve your individual report at a later date.",
"section_id": 1,
"answer": "adan13#example.net",
"shown": true
},
"119": {
"id": 119,
"type": "HIDDEN",
"question": "org",
"section_id": 1,
"answer": "",
"answer_id": 0,
"shown": true
}
}
I expect that by choosing "ip_address" for example the app would insert "99.85.42.146". But I get nothing in the field ("parameter") I chose.
enter image description here
Per my understanding , SurveyGizmo will post json data to your logic app and your logic app will accept the json data and insert part of this data into your SQL server.
Firstly, you create use a http trigger with JSON schema below :
{
"properties": {
"data_quality": {
"type": "array"
},
"date_started": {
"type": "string"
},
"ip_address": {
"type": "string"
},
"is_test_data": {},
"language": {
"type": "string"
},
"link_id": {},
"referer": {
"type": "string"
},
"response_time": {},
"session_id": {
"type": "string"
},
"survey_data": {
"properties": {
"3": {
"properties": {
"answer": {
"type": "string"
},
"id": {
"type": "integer"
},
"question": {
"type": "string"
},
"section_id": {
"type": "integer"
},
"shown": {
"type": "boolean"
},
"type": {
"type": "string"
}
},
"type": "object"
},
"119": {
"properties": {
"answer": {
"type": "string"
},
"answer_id": {
"type": "integer"
},
"id": {
"type": "integer"
},
"question": {
"type": "string"
},
"section_id": {
"type": "integer"
},
"shown": {
"type": "boolean"
},
"type": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"url_variables": {
"type": "array"
},
"user_agent": {
"type": "string"
}
},
"type": "object"
}
And then create a SQL sever insert action , with the config you need.
In this demo , I created a simple table in my sql server , I'll extract ip_address from JSON and then save it to SQL server :
As schema has been defined correctly , you can pick params you need here.
Save and run this logic app using the json data you provided, everything works as expected :
If you have any unclear , pls feel free to let me know : )
Hi, Mike , if you want to test this logic app with your custom JSON data, you can just use some some http client tools such as postman or rest client to do some test, you will get a 202 code if you triggered it successfully :
You can find the http trigger url here :
As you can see , when I post the json , a new ip record as been inserted :
Btw, I have no freelance work now, so what can I help ?

ajv does not return when validating a valid json

I have the following schema:
{
"$schema": "http://json-schema.org/schema#",
"$id": "http://api.hobnob.social/schemas/users/create.json",
"title": "Create User Schema",
"description": "For validating client-provided create user object",
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email"
},
"password": { "type": "string" },
"profile": { "$ref": "profile.json#" }
},
"required": ["email", "password"],
"additionalProperties": false
}
{
"$schema": "http://json-schema.org/schema#",
"$id": "http://api.hobnob.social/schemas/users/profile.json",
"title": "User Profile Schema",
"description": "For validating client-provided user profile object when creating and/or updating an user",
"type": "object",
"properties": {
"bio": { "type": "string" },
"summary": { "type": "string" },
"name": {
"type": "object",
"properties": {
"first": { "type": "string" },
"last": { "type": "string" },
"middle": { "type": "string" }
},
"additionalProperties": false
}
},
"additionalProperties": false
}
I am using ajv to validate against it. I am getting the expected results in almost all cases. But when validating a json with either the bio or summary fields included (with type of string), no response comes from ajv at all.
E.g. I attempt to validate
{
"email": "e#ma.il",
"password": "password",
"profile": {
"name": {
"first": "firstname"
},
"bio":"this is a bio"
}
}
and no response at all comes back.
I tried consolidating the schema but that made no difference. I'm hoping I have made some simple beginner mistake that someone may spot! I have spent many hours trying to work out what is going wrong, but after all my debugging I am no further forward.
I got this working somehow, but not sure why it started working.
In my test script I added a line to delete the test index from elasticsearch. After that, all tests passed. I then removed the new line from my test script to see if it would stop working again, but it didn't.
I'm guessing the problem was somehow related to elasticsearch...

How to specify Access Token in PostMan

I have imported a JSON file in to postman and the JSON Body is requesting Authorization Bearer value for a specific parameter. How to set a value to the parameter. Currently its undefined. Please refer the below JSON code and image.
"name": "Pos_TC-Name",
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"tests[\"Status code is 200\"] = responseCode.code === 200;"
]
}
}
],
"request": {
"url": "http://localhost:52586/api/Patient/7",
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Authorization",
"value": "Bearer {{NurseAdminToken}}",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": ""
},
"description": ""
},
"response": []
},
You have to define the variable as a global under Manage Environments You click on the "Settings Wheel" top right half of the window. Then you select/switch to the environment where you defined the global (the drop down menu next to the setting wheel).

Accept any value in POST JSON using API Blueprint

Is there any way to make POST request of API Blueprint to accept any value?
Right now I have:
### Google [POST]
Login with Google
+ token (string) - google token
+ Request (application/json)
{
"token": "google"
}
+ Response 200 (application/json)
{
"token": "xyz",
}
And nothing apart from exactly the same body as as described is being consumed by Drakov.
You can use schema, instead of hardcoded values.
Request(application/vnd.aconex.pdfmerge.job.v1.1+json)
Headers
Accept: application/vnd.pdfmerge.job.v1.1+json
Schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"zipFilePath": {
"type": "string"
},
"status": {
"type": "string"
},
"mergedFilePath": {
"type": ["string", "null"]
},
"id": {
"type": "string"
},
"fileName": {
"type": "string"
}
},
"required": [
"zipFilePath",
"status",
"mergedFilePath",
"id",
"fileName"
]
}

Swagger POST with json body

I am trying to write static .json file of server response using swagger. I'm stuck with post body and do not know how to describe it. It looks pretty much similar to Grooveshark api's where you have one page and different post parameters.
So, given grooveshark example (http://developers.grooveshark.com/docs/public_api/v3/)
Page that takes queries:
http://api.grooveshark.com/ws3.php?sig=cd3ccc949251e0ece014d620bbf306e7
POST body:
{
'method': 'addUserFavoriteSong',
'parameters': {'songID': 0},
'header': {
'wsKey': 'key',
'sessionID': 'sessionID'
}
}
How can I describe this with swagger?
without knowing a ton about how this API operates (such as, is "songID" the only parameter type?, I'm guessing you'd want something like this in your models section:
"models": {
"FavoriteSong": {
"id": "FavoriteSong",
"properties": {
"parameters": {
"type": "Parameter"
},
"header": {
"type": "Header"
}
}
},
"Parameter": {
"id": "Parameter",
"properties": {
"songID": {
"type": "integer",
"format": "int32"
}
}
}
"Header": {
"id": "Header",
"properties": {
"wsKey": {
"type": "string"
},
"sessionID": {
"type": "string"
}
}
}
}
}
And the operation would take the type "FavoriteSong" as a body type:
"parameters": [
{
"name": "body",
"description": "object to add",
"required": true,
"type": "FavoriteSong",
"paramType": "body"
}
]