I know it's possible to send post with json body in alert notification(like here Bosun send alert specifc data via json post body)
But since post with json is now deprecated in Influx I wonder if it's possible to send a post with --data-binary to write a status to specific influx series whenever an alert occurs. Writing to influx with post:
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
more on writing to influx: https://influxdb.com/docs/v0.9/guides/writing_data.html
Thanks in advance
Have you tried just setting the body of the post notification? When testing the curl command you listed above I believe it is still using content-type application/x-www-form-urlencoded which is the default for notifications. I think this would work:
notification influx{
post = http://localhost:8086/write?db=mydb
body = cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000
}
If you need to customize the values you would have to get the subject to render the correct text and use {{.}} to inject it into the post notification.
template influx.testing {
subject = `
{{ if .IsEmail }}
normal email template details here
{{else}}
{{.Group.host}} value={{.Eval .Alert.Vars.value_variable | printf "%.2f"}} {{.State.Touched.UnixNano}}
{{end}}`
body = `email body details here`
}
notification influx{
post = http://localhost:8086/write?db=mydb
body = cpu_load_short,region=us-west,host={{.}}
}
The post subject would be server01 value=0.64 1434055562000000000 assuming $value_variable is an expression that returns the 0.64 value.
also note when testing in the rule page last touched is always 0 so the timestamp would be -6795364578871345152 but it should work correctly for actual alerts.
Related
So I'm having an interesting issue with Laravel HTTP Client while trying to hit an API endpoint for PayPal.
I can get Laravel HTTP Client working on all my endpoints that POST with data, but this one endpoint that only requires headers (no data is passed in the body) fails with an error.
{
"name":"INVALID_REQUEST",
"message":"Request is not well-formed, syntactically incorrect, or violates schema.",
"debug_id":"609388c4ddfe4",
"details":[
{
"field":"\/",
"location":"body",
"issue":"INVALID_SYNTAX",
"description":"MALFORMED_REQUEST_JSON"
}
],
"links":[
{
"href":"https:\/\/developer.paypal.com\/docs\/api\/orders\/v2\/#error-INVALID_SYNTAX",
"rel":"information_link",
"encType":"application\/json"
}
]
}
When I hit the same endpoint in Postman everything works fine
My method for hitting the endpoint looks like this
public static function capture($order)
{
$token = Paypal::fetchToken();
$api_url = config('services.paypal.api_url') . '/v2/checkout/orders/' . $order['id'] . '/capture';
$headers = [
'Content/Type' => 'application/json',
];
$response = Http::withToken($token)
->withHeaders($headers)
->post($api_url)
->json();
return $response;
}
I have tried passing an empty array in the post request like this ->post($api_url, []) but that did not work either.
I have hardcoded the $api_url just in case I made a mistake with my formatting with variables. Resulted in the same issue.
I have tried changing the 'Content/Type' in the header to 'none'. This did not work either and also doesn't make sense because I have this same header set in postman and it works fine (PayPal docs also says to pass this content/type)
Based on the error I am receiving I can only assume the request is hitting the endpoint correctly, but either the HTTP wrapper or guzzle itself is adding something to the body when I leave it blank and it is causing PayPal to throw the error. Don't really know what else I can try though.
Is there a parameter I am overlooking for specifying an empty body on a post request?
Any help is appreciated.
Looking at the source I found the following solution
$response = Http::withToken($token)
->withHeaders($headers)
->send("POST", $api_url)
->json();
I had the same issue, but I fixed it with a simple trick.
I found the solution on https://docs.guzzlephp.org/en/stable/request-options.html#json.
This code should work.
$response = Http::withToken($token)
->withHeaders($headers)
->post($api_url,['json' => []])
->json();
The empty array is now seen as an empty array/body in JSON.
I have an api endpoint that I'm receiving data via a POST. My controller signature looks like this:
public function handle(Request $request)
When I go to test my endpoint, I'm running a really basic test like this:
curl -X POST -H 'Content-Type: text/xml' -d '<XML>data</XML>' http://URL/api
When I \Log::debug($request) I get nothing. Even if I \Log::debug($_POST) I still don't get anything.
Is there a filter that's turned on by default in Lumen? I'm kind of at a loss here. Maybe my curl statement is wrong?
You are sending the XML in the request body. Therefore, to retrieve the content of the request, you have to use $request->getContent like this:
public function handle(Request $request)
{
\Log::debug($request->getContent());
}
I intend to send my server response in the following format
Api Doc
I did the following
headersR.add("response_code", "OK");
headersR.add("cmd_code", "SET_FK_NAME");
headersR.add("trans_id", Long.toString(System.currentTimeMillis()/1000000));
JSONPObject map1 = new JSONPObject("fk_name", "jj");
return new ResponseEntity<>(map1, headersR, HttpStatus.OK);
I was getting a negative response from the other side so I checked Wireshark(Had a hard time logging my response body). And I got this in Wireshark.
Wirehark Screenshot
The response body is Definitely not JSON.
How can I fix this?
The response body fk_name("jj") is not JSON, it's JSONP -- Browser would take the function name fk_name and try to execute it with "jj" as parameter.
The root cause is you are using JSONPObject, whose constructor accepts 2 parameters: a function name, and the data value. Not the expected JSON key and value.
To fix this issue and return {"fk_name":"jj"}, remove the JSONPObject stuff and use code as follow:
return new ResponseEntity<>("{\"fk_name\":\"jj\"}", headersR, HttpStatus.OK);
I am trying to call the ValuesController WebApi controller (that gets created by default) "PUT" method using cURL. No matter what I do, send value="abc", "abc" or "=abc" as I saw suggested in other questions for the POST method, to no avail.
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
I also tried changing the Content-Type to application/json and application/x-www-form-urlencoded, nothing seems to work.
curl -H -d "=TEST" -X PUT http://localhost:30960/api/values/5
Is this a bug in ASP.NET 5 Web Api or is there a different format I need to use when calling?
In order to make it work you need to pass it as a simple string:
"any value here"
There is even a test for this behaviour
Remember to add an application/json Content-Type header when issuing a request.
I've just tested it for both PUT and POST requests.
Using curl it's:
curl -H "Content-Type: application/json" -d "'Test'" -X PUT http://localhost:[port]/api/values/5
If you are posting data as application/x-www-form-urlencoded then you need to remove the FromBody attribute. This is a change in behavior from MVC5/WebAPI2.
If you are posting data as application/json, then you need the FromBody and also a bindable typ. Example:
public class Person
{
public string Name {get; set;}
}
for data { "Name" : "James"}
How can I use Fiddler to check the response from a web server. I could easily check the GET method by pasting the url to the field in Request Builder and get the response back in xml/json. There is an option POST, however I don't know how can I pass the parameters to the POST.
For example:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin");
request.Method = "POST";
string postData = "accountType=HOSTED_OR_GOOGLE";
postData += "&Email=yourusername#gmail.com";
postData += "&Passwd=yourpassword";
postData += "&service=finance";
postData += "&source=test-test-.01";
How can I pass my Data into this POST method in Fiddler to get the response?
The simplest way to do this is to have Fiddler capture an instance of this request and drag/drop that session onto the Request builder.
But generating a post yourself isn't hard. Set the RequestBuilder's method to POST, add a header:
Content-Type: application/x-www-form-urlencoded
And put in the Request Body the text of the post:
accountType=HOSTED_OR_GOOGLE&Email=yourusername#gmail.com&Passwd=yourpassword&service=finance&source=test-test-.01
Step 1: Composer with Http Post, URL, Header and Body
Step 2: And Result console.log at Server with Json