how to design dynamic response in mock server - dynamic

I'm now using the mock server from https://www.mock-server.com/ and run it in a docker container.
Now I would like to let the response change as request body changes. I've looked up for dynamic response on official webiste for a while, but have no idea on how to extract specific data from request body.
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest": {
"path": "/some/path"
},
"httpResponseTemplate": {
"template": "return { statusCode: 200, body: request.body };",
"templateType": "JAVASCRIPT"
}
}'
The code above is to create a simple expectation, which will response the request body. For example,
$curl http://localhost:1080/some/path -d '{"name":"welly"}'
{"name":"welly"} //response
Now I want to change the way of giving the response. For example, I would like to input {a:A, b:B} and get the response {a:B, b:A}.
So, how to modify the json data in request body and give it to response? I guess there are some methods to extract specific data from json file, or modify json data, etc. Also, I want to know how to better search the detailed information, since the official website and full REST API json specification
(https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/5.11.x#/expectation/put_expectation) is hard for me to understand.
Thanks a lot!

I needed to do this as well, I think I got this to work have a look at my curl example for the expectation I hope that helps:
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest": {
"path": "/api/fun",
"method": "POST"
},
"httpResponseTemplate": {
"template": "
req = JSON.parse(request.body.string)
rid = req[\"id\"]
return { statusCode: 201, body: {new_id: rid} }
",
"templateType": "JAVASCRIPT"
}}'
After you do this if send:
curl -X POST http://localhost:1080/api/fun --data '{"id": "test_1"}'
it should return:
{ "new_id" : "test_1" }
Javascript templating supported via Nashorn engine and thus will no longer be available from Java 15. Here is the note from the Mockserver docs.
From Java 15 Nashorn is no longer part of the JDK but it is available as a separate library that requires Java 11+. Once MockServer minimum Java version is 11 then this separate library will be used.

Related

API Authentication issues from bubble to claifai

I have built a Bubble App and need to integrate it with a application custom model I am building in Clarifai but when I use the Bubble API plugin to connect to do an image predict it says I have an authentication issue 10002
Bubble API header details
Bubble POST call
what am I doing wrong???
Please try using the following cURL command and replacing it with your variable. I have tested it on my side and it works. As for the Bubble issue, that is something you would need to contact their Support about. We are not sure how they are processing their requests but Bubble doesn't seem to able to handle API calls from Clarifai.
curl -X POST \
-H "Authorization: Key YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '
{
"inputs": [
{
"data": {
"image": {
"url": "https://samples.clarifai.com/metro-north.jpg"
}
}
}
]
}'\
https://api.clarifai.com/v2/models/{YOUR_MODEL_NAME}/outputs

How to POST json parameters from Postman to Jenkins?

I need to call a Jenkins job using its API through Postman. This job requires parameters (HOST, VERBOSITY and PMSP).
Auth works using Jenkins token and header Content-type:application/json is used.
I tried to call the endpoint https://jenkins_server/job/job_name/build/api/json adding the following body to the request but the result is Nothing is submitted, and the job doesn't run.
I tried to call the endpoint https://jenkins_server/job/job_name/buildWithParameters/api/json adding the same body. I get 201 Created (job is running) but no parameters are given to the job.
{
"parameter": [
{
"name": "HOSTS",
"value": "[linux]\n1.2.3.4"
},
{
"name": "VERBOSITY",
"value": "vv"
},
{
"name": "SANS_PMSP",
"value": true
}
]
}
Is my JSON well constructed ? Which endpoint do I need to call ?
If it's Postman that you would like to focus on, you can import the curl command straight into the application.
This creates a new request for you to use and it populates this request, based on the details in the command.
From here, you should be able to add your own URL and point this at the location you need.

Using Taxee.io API

I'm trying to access the Taxee.io API using the request npm module. The documentation is slightly poor and the difference between the Mashape info and the website's info is confusing.
https://taxee.io/
The docs have one example of a request here.
curl 'https://taxee.io/api/v2/calculate/2017' -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJBUElfS0VZX01BTkFHRVIiLCJodHRwOi8vdGF4ZWUuaW8vdXNlcl9pZCI6IjU4NDQ4MTA4Mzg2NjhhMTU4ZDU0ZmIzNSIsImh0dHA6Ly90YXhlZS5pby9zY29wZXMiOlsiYXBpIl0sImlhdCI6MTQ5OTA1MzU0NX0.pOwC5JEC7trLaaZVgHHGu_rvN0-EGa3RMm8BgJ-M9gk' -H 'Content-Type: application/x-www-form-urlencoded' --data 'state=NC&filing_status=married&pay_periods=26&pay_rate=116500&exemptions=2'
I however want to use the request npm module and am struggling to bridge the gap in how it will work in my express app.
const request = require('request');
request.post('https://taxee.io/api/v2/calculate/2017', {
'auth': {
'Bearer': 'mykey'
}
});
This is what I have thus far. Any help is appreciated.
Keep in mind that properties are case sensitive in JavaScript. You must pass the bearer token under the key bearer and not Bearer.
To replicate the Content-type and pass data, use the form support of the library.
E.g. like this:
{
auth: {
bearer: '<token>',
},
form: {
state: 'NC',
// ...
},
}

API ignoring my input on POST

I am sending this body via API POST to extractor I created with app.
{"input":{"website/url":"http://www.targetsite.com/"}}
No matter what URL I put in, it always returns me the results from the same page (I think it must be the one I trained it on). I can put any string in there and it just totally ignores it.
GET works fine but I would much rather use POST.
Anyone know why this isn't working?
this should work, here is a worked example
curl -b authcookie -XPOST 'https://api.import.io/store/data/f53e3905-c5d0-457a-befd-d13a3a421bcf/_query' -d '
{
"input": {
"webpage/url": "http://www.ikea.com/us/en/search/?query=chair"
}
}
'
compared to:-
curl -b authcookie -XPOST 'https://api.import.io/store/data/f53e3905-c5d0-457a-befd-d13a3a421bcf/_query' -d '
{
"input": {
"webpage/url": "http://www.ikea.com/us/en/search/?query=light"
}
}
'
table versus light results. If you look in the json of the response it should tell you the url that the results are returned from:-
"pageUrl": "http://www.ikea.com/us/en/search/?query=light",
You can also try out this yourself on our api docs:-
http://api.docs.import.io/#!/Query_Methods/queryPost

Shopify returning "topic can't be blank" for webhook POST even though I'm supplying one

I'm trying to create a webhook here:
curl --header "X-Shopify-Access-Token: <token>" -d '{"webhook": {"topic": "orders/create", "address": "http://www.google.com/", "format": "json"}}' https://test-store-402.myshopify.com/admin/webhooks.json
Here's the JSON pretty-printed for readability:
{
"webhook": {
"topic": "orders/create",
"address": "http://www.google.com/",
"format": "json"
}
}
It's returning this error:
{"errors":{"topic":["can't be blank","Invalid topic specified. Topics allowed: orders/create, orders/updated, orders/paid, orders/cancelled, orders/fulfilled, orders/partially_fulfilled, app/uninstalled, customer_groups/create, customer_groups/update, customer_groups/delete, products/create, products/update, products/delete, collections/create, collections/update, collections/delete, carts/create, carts/update"],"address":["can't be blank"]}}
I've confirmed:
The spelling of the topic is correct
The JSON request payload is wrapped in "webhook" (singular) correctly, as per the docs
I've copied and pasted the JSON into jsonlint.com to sanity check that the JSON is valid
Confused...didn't see any other questions on SO related to this specifically, I must be doing something terribly obvious wrong - or else everyone would be hitting this issue.
UPDATE: I got it to work over command-line by passing in the Content-type: application/json header. But now I'm having problems over curl_exec in PHP. I have the following CURLOPT's set:
CURLOPT_RETURNTRANSFER: true
CURLOPT_SSL_VERIFYPEER: false
CURLOPT_FOLLOWLOCATION: true
CURLOPT_MAXREDIRS: 10
CURLOPT_CUSTOMREQUEST: 'POST'
CURLOPT_HTTPHEADER: array {
0 => string 'X-Shopify-Access-Token: <token>'
1 => string 'Content-type: application-json' (length=30)
Note that I can do GET's just fine over curl - only POSTs returning this confusing response.
Got it to work in curl_exec. Here's what I'm using:
CURLOPT_RETURNTRANSFER: true
CURLOPT_SSL_VERIFYPEER: false
CURLOPT_FOLLOWLOCATION: true
CURLOPT_MAXREDIRS: 10
CURLOPT_CUSTOMREQUEST: 'POST'
CURLOPT_HTTPHEADER: array {
0 => string 'X-Shopify-Access-Token: <token>'
1 => string 'Content-type: application-json' (length=30)
CURLOPT_POSTDATA: '{"webhook":{"topic":"orders/updated","address":"http://www.google.com","format":"json"}}'
I think the problem may have been that I left out the CURLOPT_POSTDATA in my Update above.