wso2 Could not save JSON payload. Invalid input stream found - json

This is my very first REST API in WSO2, the sequence in wso2 as below.
<? xml version = "1.0" encoding = "UTF-8" ?>
< api xmlns = "http://ws.apache.org/ns/synapse"
name = "createIssue"
context = "/rest"
hostname = "XXX"
port = "XXX" >
< resource methods = "POST"
inSequence = "createInSequence"
`enter code here`
outSequence = "createOutSequence" >
< faultSequence / >
< /resource>
</api >
<? xml version = "1.0"
encoding = "UTF-8" ?>
< sequence xmlns = "http://ws.apache.org/ns/synapse"
name = "createInSequence" >
< log level = "custom" >
< property name = "location"
expression = "json-eval($.fields)" / >
< /log>
</sequence >
The curl command that I am making to invoke the api, is as below.
'curl -X POST -H "Content-Type:application/json" -d#"h:\createissue_own.json" "http://ip:port/rest/api/createissue"'
WSo2 esb Console shows the following error
newJsonPayload. Could not save JSON payload. Invalid input stream found.'
The input json, is a valid json, which I have validated and given below.
{
"fields": {
"project": {
"id": "10301"
},
"summary": "Issue",
"description": "Description text",
"issuetype": {
"id": "10205"
},
"customfield_10600": {
"id": "10300"
},
"customfield_10602": {
"id": "10301"
},
"customfield_10603": "ID text",
"customfield_10608": {
"id": "10303"
},
"customfield_10609": " text",
"customfield_10610": " text",
"customfield_10611": " text",
"customfield_10612": "Postcode text"
}
}
Any suggestions please. I am not able to hit the url, and log the input json.

This error usually comes when you send XML payload with application/json Content-Type header to the ESB. The best thing we can do here is enable the wirelogs and check the HTTP headers and payloads sent to the ESB. To enable wirelogs follow these steps,
Shutdown the ESB instance
Move onto the $ESB_HOME/repository/conf directory and locate the
log4j.properties file Then uncomment the following line
log4j.logger.org.apache.synapse.transport.http.wire=DEBUG
Then restart the ESB instance again and send a request
Monitor the ESB console for wirelogs as follows
The symbol '>>' gives the contents written into the ESB and '<<' gives the contents written out of the ESB.
Also I doubt your curl command, hence a sample curl command is given below.
curl -v -d #test.json -H "Content-Type:application/json" http://localhost:8280/rest/api/createissue

Related

Powershell Nested Json doesn't look like a Json

I am currently trying to prepare a JSON body for an API call, which should look something like this
curl -XPOST -H 'Authorization: Bearer ***API*KEY***' -H 'Content-Type: application/json' http://127.0.0.1:9000/api/alert -d '{
"title": "Other alert",
"description": "alert description",
"type": "external",
"source": "instance1",
"sourceRef": "alert-ref",
"severity": 3,
"tlp": 3,
"artifacts": [
{ "dataType": "ip", "data": "127.0.0.1", "message": "localhost" },
{ "dataType": "domain", "data": "thehive-project.org", "tags": ["home", "TheHive"] },
],
"caseTemplate": "external-alert"
}'
The problem however is that my json body which I create with powershell has weird characters and don't see where the problem is. This is my JSON Body
{
"tlp": 1,
"source": "Test",
"title": "Test Alert1",
"artifacts": "{\r\n \"dataType\": \"ip\",\r\n \"data\": \"127.0.0.1\"\r\n}",
"type": "external",
"sourceRef": "1",
"description": "Test",
"severity": 1
}
I create this JSON body as follows. I have already tried CustomObjects and Hashtables, but always get the same output
$body = #{
title = "$title"
description = "$Alert_Description"
type ="external"
source ="$Source"
sourceRef ="$SourceRef"
severity = $Severity
tlp = $tlp
$artifacts = [PSCustomObject]#{
dataType=ip
data=127.0.0.1}
}| ConvertTo-Json
$JsonBody = $body | ConvertTo-Json
Can somebody give me a hint? I have no clue anymore
First, fix your hashtable so that it creates the intended JSON representation, and only apply ConvertTo-Json once:
$body = [ordered] #{
title = $title
description = $Alert_Description
type = 'external'
source = $Source
sourceRef = $SourceRef
severity = $Severity
tlp = $tlp
artifacts = , [pscustomobject] #{
dataType = 'ip'
data = '127.0.0.1'
}
} | ConvertTo-Json
Variables don't need enclosing in "..." unless you explicitly want to convert their values to strings.
Literal textual information such as ip does require quoting ('...', i.e. a verbatim string literal, is best).
The artifacts property is an array in your sample JSON, so the unary form of ,, the array constructor operator is used to wrap the [pscustomobject] instance in a single-element array.
If you have multiple objects, place , between them; if the array was constructed previously and stored in a variable named, say, $arr, use artifacts = $arr.
Use of [ordered], i.e. the creation of an ordered hashtable isn't strictly necessary, but makes it easier to compare the resulting JSON representation to the input hashtable.
Generally keep in mind that explicit use of a -Depth argument with ConvertTo-Json is situationally required in order to ensure that properties aren't truncated - see this post for more information. However, with the hashtable in your question this happens not to be a problem.
Second, since you're sending the json to an external program, curl (note that in Windows PowerShell you'd have to use curl.exe in order to bypass the built-in curl alias for Invoke-WebRequest, an additional escaping step is - unfortunately - required up to at least v7.1 - though that may change in v7.2: The " instances embedded in the value of $body must manually be escaped as \", due to a long-standing bug in PowerShell:
curl -XPOST -H 'Authorization: Bearer ***API*KEY***' -H 'Content-Type: application/json' `
http://127.0.0.1:9000/api/alert -d ($body -replace '([\\]*)"', '$1$1\"')
Note: With your particular sample JSON, -replace '"', '\"' would do.
See this answer for more information.
Slap a "-Depth 50" on your ConvertTo-Json, that should solve your issue.
Like here: ConvertTo-Json flattens arrays over 3 levels deep

CURL Get download link from request and download file

I'm using conversocial API:
https://api-docs.conversocial.com/1.1/reports/
Using the sample from the documentation, as after all tweaks I receive this "output"
{
"report": {
"name": "dump", "generation_start_date": "2012-05-30T17:09:40",
"url": "https://api.conversocial.com/v1.1/reports/5067",
"date_from": "2012-05-21",
"generated_by": {
"url": "https://api.conversocial.com/v1.1/moderators/11599",
"id": "11599"
},
"generated_date": "2012-05-30T17:09:41",
"channel": {
"url": "https://api.conversocial.com/v1.1/channels/387",
"id": "387"
},
"date_to": "2012-05-28",
"download": "https://s3.amazonaws.com/conversocial/reports/70c68360-1234/#twitter-from-may-21-2012-to-may-28-2012.zip",
"id": "5067"
}
}
Currently, I can sort this JSON output to download only and will receive this output
{
"report" : {
"download" : "https://s3.amazonaws.com/conversocial/reports/70c68360-1234/#twitter-from-may-21-2012-to-may-28-2012.zip"
}
}
Is it anyway of automating this process by using CURL, to make curl download this file?
To download I'm planning to use simple way as:
curl URL_LINK > FILEPATH/EXAMPLE.ZIP
Currently thinking is there is a way to replace URL_LINK with download link?? Or any other way, method, way around????
Give a try to this:
curl $(curl -s https://httpbin.org/get | jq ".url" -r) > file
Just replace your url and the jq params, based in your json, thay may be:
jq ".report.download" -r
The -r will remove the double quotes "
The way it works is by using a command substitution $():
$(curl -s https://httpbin.org/get | jq ".url" -r)
This will fetch you URL and extract the new URL from the returned JSON using jq the one later is passed to curl as an argument.

Error when posting JSON using NiFi vs. curl

I am seeing a very slight difference between how NiFi's InvokeHTTP processor POSTs json data and how curl does it.
The problem is that the data APPEARS to be the same when I log it ... but the data is rendering differently.
Does anyone have any idea what could be wrong? Thank you!
CURL -- works; correct printout & render
curl -X POST -H "Content-Type: application/json" -d '{ "responseID": "a1b2c3", "responseData": { "signals": [ "a", "b", "c" ] } } localhost:8998/userInput
WebServer app printout
responseID: a1b2c3
responseData: {signals=[a, b, c]}
Template render
NiFi -- does not work; correct printout BUT incorrect render
Generate FlowFile
UpdateAttributes
AttributesToJSON
InvokeHTTP
WebServer app printout
responseID: a1b2c3
responseData: {signals=[a, b, c]}
Template render
you need this kind of json:
{ "responseID": "a1b2c3", "responseData": { "signals": [ "a", "b", "c" ] } }
but in nifi you building this:
{ "responseID": "a1b2c3", "responseData": "{ signals=[ a, b, c ] }" }
it means that you create responseData just as a string "{ signals=[ a, b, c ] }" but you need an object
in nifi the AttributesToJSON processor creates only one level object, so you can create a sequence of AttributesToJSON -> EvaluateJsonPath -> AttributesToJSON to make nested json objects.
or use ExecuteStript with javascript or groovy language - both has good syntax to build json.

RabbitMQ REST HTTP JSON payload

I am trying to use RabbitMQ HTTP REST client to publish messages into the queue. I am using the following url and request
http://xxxx/api/exchanges/xxxx/exc.notif/publish
{
"routing_key":"routing.key",
"payload":{
},
"payload_encoding":"string",
"properties":{
"headers":{
"notif_d":"TEST",
"notif_k": ["example1", "example2"],
"userModTime":"timestamp"
}
}
}
And getting back from the rabbit the following response:
{"error":"bad_request","reason":"payload_not_string"}
I have just one header set:
Content-Type:application/json
I was trying to set the
"payload_encoding":"base64",
but it didn't help. I am new to rabbit any response is welcome.
Try with
{
"properties": {
"content-type": "application/json"
},
"routing_key": "testKey",
"payload": "1234",
"payload_encoding": "string"
}
Working example. We need simple to escape doublequotes.
It is important that the colon is outside of the quotes, as this causes inexplicable errors.
{
"properties": {},
"routing_key": "q_testing",
"payload": "{
\"message\": \"message from terminal\"
}",
"payload_encoding": "string"
}
I managed to send content-type using underscore "_" instead of dash.
See here for list of valid properties.
See RabbitMQ Management HTTP API for some examples.
To publish a json message using curl to rabbit exchange:
curl -i -u guest:guest -XPOST --data '{"properties":\
{"content_type":"application/json"}, \
"routing_key":"", \
"payload":"{\"foo\":\"bar\"}",\
"payload_encoding":"string"}' \
"http://localhost:15672/api/exchanges/%2f/exchange_name/publish"
content_type is written using underscore, routing_key is empty to send a message to exchange, not to particular queue.
To use a JSON formatted payload you have to encode it in base64 and use the "payload_encoding": "base64" attribute.

Invalid payload - Slack API JSON

I am using a Groovy script to send a POST using the Slack API, at present I am getting invalid_payload returned and I think this is most likely due to the formatting of my JSON. I know the Slack API expects it JSON with double quotes but I can't seem to be able to pass a variable into the JSON object:
SUCCESS_MESSAGE = '{"attachments": [{"color": "#2A9B3A", "author_name": ${DEV_NAME}, "title": "Build Status", "title_link": ${BUILD_URL}, "text": "Successful Build" }]}'
def response = ["curl", "-X", "POST", "-H", "Content-Type: application/json", "-d", "${SUCCESS_MESSAGE}", "https://hooks.slack.com/services/${SLACK_WEBHOOK}"].execute().text
How should I correctly format my SUCCESS_MESSAGE var so I don't get the error?
You need to quote your DEV_NAME and BUILD_URL variable expansions so the JSON string is valid.
Your whole string needs to be enclosed in " instead of ' so the variables are actually expanded
And you need to escape the " inside your string so they appear in your JSON string.
SUCCESS_MESSAGE = "{\"attachments\": [{\"color\": \"#2A9B3A\", \"author_name\": \"${DEV_NAME}\", \"title\": \"Build Status\", \"title_link\": \"${BUILD_URL}\", \"text\": \"Successful Build\" }]}"`
Alternatively you can generate the JSON in much nicer programmatic way. Which would be helpful if your notifications got a bit more complicated:
def notification = [
attachments: [
[
color: "#2A9B3A",
author_name: DEV_NAME,
title: "Build Status",
title_link: BUILD_URL,
text: "Successful Build"
]
]
]
def response = ["curl", "-X", "POST", "-H", "Content-Type: application/json", "-d", JsonOutput.toJson(notification), "https://hooks.slack.com/services/${SLACK_WEBHOOK}"].execute().text