Swagger API - multiple #ApiImplicitParam for JSON POST - json

I am trying to create my first REST app and I am using Swagger to document and test it. My controller receives POST in JSON:
"body": {
"search_query" : "text",
"page" : 1
}
I thought I can annotate controller like this:
#ApiImplicitParams({
#ApiImplicitParam(name="search_query",value="Text to search for",required = false,dataType = "String",paramType = "body"),
#ApiImplicitParam(name="page",value="Page number",required = true,dataType = "int",paramType = "body")
})
But generated curl looks like this:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '1' 'http://localhost:9000/api/search'
And this is not what I am looking for.. , then I found out I can not have 2 body parameters. But how do I annotate my controller then , if I do not want to create class for this data and annotate it with model?

Related

Shell script not working for curl request

I want to make a curl request with Databox to push somemetrix and want to do it in shell script.
Here is the databox POST request example (which works like a charm)
curl https://push.test \
-u token
: \
-X POST \
-H 'Content-Type: application/json' \
-H 'Accept: application/vnd.databox.v2+json' \
-d '{
"data":[
{
"$testcount": 50,
"test_name": "test"
}
]
}'
When I form the json body as a separate json string and try to pass as parameter, it doesn't work and gives a json parsing error. I am not sure what am I am doing wrong here. can someone help? I am new to shell scripts
#!/bin/bash
JSON_STRING= '{"data" : [{"$testcount":50,"testname":"test"}]}'
echo "$JSON_STRING"
curl https://testpush \
-u token
: \
-X POST \
-H 'Content-Type: application/json' \
-H 'Accept: application/vnd.databox.v2+json' \
-d '$JSON_STRING'
error :
{"type":"invalid_json","message":"Invalid request body - JSON parse error"}
I have added my token for the request, so the authorisation should work.
You have excess whitespace around the =.
Also, $JSON_STRING in the last line of the second script should be in double quotes instead of the single quotes, to get it expanded into what you just set it to.
Btw., if data gets out of hand or is sensitive, you might want to look into the possibility to start the data with the letter # and then have the rest be a file name that contains the data.

Unable to substitute json from a file into a json data passed to curl in bash

I have a CloudFormation script that contains a json content that I need to substitute for a json field within the post body or data of the curl POST request that I will make.
The CloudFormation file is like this:
{ "AWSTemplateFormatVersion": "2010-09-09", "Description":
...}
The problem is that I have tried some code below but it is not working.
However, I copy and past the content of the CloudFormation file into my POST request's body it works as expected. This implies that this is a substitution or scripting problem.
CLOUD_FORMATION_FILE=/home/developer/workspace/blah/blah/infrastructure/templates/component.json
template=`cat $CLOUD_FORMATION_FILE`
echo $template
curl -d '{"template": $(echo $template)}' \
-H 'Content-Type: application/json' https://base.url.com/v1/services/component-proxy/test/stacks/test-component-proxy-component \
--cert /etc/pki/tls/certs/client.crt --key /etc/pki/tls/private/client.key
I am getting the error:
{"error": "Invalid JSON. Expecting object: line 1 column 13 (char 13)"}
You need to use double quotes in order to substitute a variable in a string.
There's no need to use $(echo $variable), just use $variable.
curl -d "{\"template\": $template}" \
-H 'Content-Type: application/json' https://base.url.com/v1/services/component-proxy/test/stacks/test-component-proxy-component \
--cert /etc/pki/tls/certs/client.crt --key /etc/pki/tls/private/client.key

Correct format for multiline '--data' in curl query to graphql endpoint?

I'm passing "--data" via curl to a GraphqQL API endpoint.
I want to be able to pass the data in 'prettified' form, e.g. as in GraphiQL browser,
{
alpha {
param1
param2
}
}
Atm, my formatting inside the data -- namely, re: line returns -- isn't handled properly.
This single-line-string form works,
curl \
-H 'content-type: application/json' \
-X POST /path/to/graphql/api/endpoint \
--data '{ "query":
"query { alpha {param1, param2} } "
}'
This 'prettified' version does not
curl \
-H 'content-type: application/json' \
-X POST /path/to/graphql/api/endpoint \
--data '{ "query":
"query {
alpha {
param1
param2
}
} "
}'
What's the right syntax for passing the 2nd form?
I'm guessing some combination of quoting/escaping?
Newlines just aren't allowed inside JSON strings. (See RFC 8259 ยง7, which states that control characters must be escaped.) You can turn a newline into \n, but that gets a little unwieldy:
curl \
-H 'content-type: application/json' \
-X POST /path/to/graphql/api/endpoint \
--data '{ "query":
"query {\n alpha {\n param1\n param2\n }\n} "
}'
Since the JSON queries are pretty well-structured, it seems to work better to use a dedicated tool for submitting GraphQL queries: the standalone version of GraphiQL is an okay default, or if you have a favorite scripting language with a reasonable HTTP client it's easy enough to write something with that.
Something along the lines of this:
QUERY='{ "query":
"query {
alpha {
param1
param2
}
} "
}'
curl \
-H 'content-type: application/json' \
-X POST /path/to/graphql/api/endpoint \
--data "$(echo $QUERY)"

Grails command object and 404 when space inside JSON from CURL

I have inherited a controller.
When a post request is made, with a well structured JSON document everything is fine.
When the JSON contains a space in a feld, 404 is returned.
However, when the same request is made from mozilla restclient extension everything works.
The CURL request specifically is:
curl --include \
--request POST \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
--data-binary "{
"planCode" : "My Test App-standard"
}" \
"https://localhost/signupApi/v2/signup"
URLMapping:
"/signupApi/v2/$action"{
parseRequest = true // parse json, and assign to params
controller = "signupApiSignup"
}
So, why would a space in curl cause problems in the request body that grails receives?
Thanks
You are not quoting your strings there properly in your shell. Use ' for your parameters, if you plan to use "sensitive" chars like " there. Or use \" inside. Also curl can read a filename if you prefix it with # instead of the actual data.
Yet in this case maybe quoting with ' is easiest. E.g.:
...
--data-binary '{
"planCode" : "My Test App-standard"
}'
...

Shopify fulfillment

The online shopify docs for fulfillment show this example:
POST /admin/orders/#{id}/fulfillments.json
{
"fulfillment": {
"tracking_number": null,
"line_items": [
{
"id": 466157049
}
]
}
}
The docs also say that not specifying an item id will cause all items to fulfill.
My postdata to that api endpoint reads:
{"fulfillment":{"tracking_number":null}}
This comes back:
{"errors":{"fulfillment":"can't be blank"}}
I have tried this:
{"fulfillment":{"tracking_number":null,"line_items":[{"id":300668234}]}}
which is a valid item id for my order. The same message comes back.
Ideas?
Are you setting your Content-Type to application/json?
Trying sending the same request using cURL
curl -X POST -H 'Content-Type: application/json' -d #fulfillment.json https://API_KEY:API_TOKEN#SHOP.myshopify.com/admin/orders/ORDER_ID/fulfillments.json
If that works, it means you aren't sending your request correctly.
curl -H "Content-Type: application/json" -X POST -d '{"fulfillment": {"tracking_number": "123456789","notify_customer": true}}' https://API_KEY:TOKEN#SHOP.myshopify.com/admin/orders/ORDER_ID/fulfillments.json
This is the working example of updating tracking number for an order. It took me a minute to realize that the ORDER_ID is the long form shopify order id. It should be about 9 to 10 digits.