i have created an api in java that accepts (name="sample" multipart[]) and when i'm requesting from php curl,
curl file objects in the array overwrites the old one to new, it means, only one value in the array is accepted.
This is one of the code that i tried:
$files = array(
'path/sample1.png',
'path/sample2.png',
);
$postfields = array();
foreach ($files as $index => $file) {
if (function_exists('curl_file_create')) {
$file = new CURLFILE(realpath($file), mime_content_type($file), basename($file));
}
else {
$file = '#' . realpath($file);
}
$postfields["sample"] = $file;
}
Note: I already tried sending a request in postman and it works perfectly with same keyname. The generated code have the same keyname in the post fields like this..
"sample" => "",
"sample" => ""
I don't know what to follow since the postman request is success, i tried also using the generated code from postman but the output is the same as stated above.
CURL:
CURLOPT_HEADER => 0,
CURLOPT_VERBOSE => 0,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
Related
this is my conf. file
input {
file {
type => "java"
path => "/UUUU********/IdeaProjects/elk-stack-logging-example/elk-example.log"
codec => multiline {
pattern => "^%{YEAR}-%{MONTHNUM}-%{MONTHDAY} %{TIME}.*"
negate => "true"
what => "previous"
}
}
}
filter {
#If log line contains tab character followed by 'at' then we will tag that entry as stacktrace
if [message] =~ "\tat" {
grok {
match => ["message", "^(\tat)"]
add_tag => ["stacktrace"]
}
}
grok {
match => [ "message",
"(?<timestamp>%{YEAR}-%{MONTHNUM}-%{MONTHDAY} %{TIME}) %{LOGLEVEL:level} %{NUMBER:pid} --- \[(?<thread>[A-Za-z0-9-]+)\] [A-Za-z0-9.]*\.(?<class>[A-Za-z0-9#_]+)\s*:\s+(?<logmessage>.*)",
"message",
"(?<timestamp>%{YEAR}-%{MONTHNUM}-%{MONTHDAY} %{TIME}) %{LOGLEVEL:level} %{NUMBER:pid} --- .+? :\s+(?<logmessage>.*)"
]
}
date {
match => [ "timestamp" , "yyyy-MM-dd HH:mm:ss.SSS" ]
}
}
output {
# Sending properly parsed log events to elasticsearch
elasticsearch {
hosts => ['https://**********************:9243/']
user => 'elastic'
password => '*********************'
index => "logstash_%{+YYYYMMdd}"
}
stdout { codec => rubydebug }
}
after running the code I go to elastic search then I want to create a data view inserting the name log stash it says not data stream or index found for a given name.
How to make this conf file work and create that index so I can create the view
I Have a file json named production_2.json
[
{
"v":{
"id":"rep_01564526",
"name":"tuttoverde.pgmx",
"type":"PRODUCTION_STARTED",
"ute":"CDL",
"ver":"1.0",
"status":"EXE"
},
"ts":"2020-11-19T08:00:00Z"
},
{
"v":{
"id":"rep_01564526",
"name":"tuttoverde.pgmx",
"type":"PRODUCTION_ENDED",
"ute":"CDL",
"ver":"1.0",
"status":"EXE"
},
"ts":"2020-11-19T17:00:00Z"
}
]
And have the folling Karate code, that:
Read the file production_2.json
and for each element of the array send a topic
I * def sendtopics =
"""
function(i){
var topic = "data." + machineID + ".Production";
var payload = productions[i];
karate.log('topic: ', topic )
karate.log('payload: ', payload )
return mqtt.sendMessage(payload, topic);
}
"""
* def productions = read('this:productions_json/production_2.json')
* def totalProductionEvents = productions.length
* def isTopicWasSent = karate.repeat(totalProductionEvents, sendtopics)
* match each isTopicWasSent == true
The function
mqtt.sendMessage(payload, topic);
is a function in java, that have the following segnature
public Boolean sendMessage(String payload, String topic) {
System.out.println("Publishing message: ");
System.out.println("payload " + payload);
System.out.println("topic " + topic);
return true;
}
the problem is that the value of the "payload" inside the javascript function is correct and is printed correctly, while inside the "sendMessage" function the value of the payload is formatted incorrectly.
For example here is what it prints inside karate.log('payload: ', payload )
payload: {
"v": {
"id": "rep_01564526",
"name": "tuttoverde.pgmx",
"type": "PRODUCTION_STARTED",
"ute": "CDL",
"ver": "1.0",
"status": "EXE"
},
"ts": "2021-01-08T08:00:00Z"
}
And Here instead what is printed on the function "sendMessage" of the java class
payload {v={id=rep_01564526, name=tuttoverde.pgmx, type=PRODUCTION_STARTED, ute=CDL, ver=1.0, status=EXE, ts=2021-01-08T08:00:00Z}
I don't understand why the payload is formatted incorrectly (= instead of : ) and is it not a string. I also tried using the following solution and it doesn't work for me
* def sendtopics =
"""
function(i){
var topic = "data." + machineID + ".Production";
var payload = productions[i];
var payload2 = JSON.stringify(payload);
return mqtt.sendMessage(payload2, topic);
}
"""
How do I convert an object inside javascript to a string so I can pass it to java?
You are doing some really advanced stuff in Karate. I strongly suggest you start looking at the new version (close to release) and you can find details here: https://github.com/intuit/karate/wiki/1.0-upgrade-guide
The reason is because the async and Java interop has some breaking changes - and there are some new API-s you can call on the karate object in JS to do format conversions:
var temp = karate.fromString(payload);
And karate.log() should work better and not give you that odd formatting you are complaining about. With the current version you can try karate.toJson() if that gives you the conversion you expect.
Given your advanced usage, I recommend you start using the new version and provide feedback on anything that may be still needed.
I am using the bellow code to retrieve data from an API:
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=> "Cookie: JSESSIONID=CF0D1FA323B4F3FCEF90BA3EE4651CAC\r\n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://webfeeder.cedrofinances.com.br/services/quotes/quote/vale5', false, $context);
The above code returns the following error message:
Warning: file_get_contents(http://webfeeder.cedrofinances.com.br/services/quotes/quote/vale5): failed to open stream: HTTP request failed! HTTP/1.1 405 Method Not Allowed in
The API uses REST (HTTP). The above method is describe as GET on documentation.
I've also tried to use PHP CURL. But the same error message is shown.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "http://webfeeder.cedrofinances.com.br/services/quotes/quote/vale5",
CURLOPT_HTTPHEADER => array("Cookie: JSESSIONID=CF0D1FA323B4F3FCEF90BA3EE4651CAC"),
));
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
The API documentation shows the following JAVA code to retrieve this same data:
public void getQuote(){
GetMethod getMethod = new GetMethod ("http://webfeeder.cedrofinances.com.br/services/quotes/quote/vale5");
String result = HttpUtils.get(getMethod);
System.out.println(resutl);
}
this code is in php where there are different type of json objects in an array
function data($text, $number, $user_id, $port = NULL, $encoding = NULL)
{
$data = array
(
"text" => "#param#",
"param" => array
(
array(
"number" => $number,
"text_param" => array($text),
"user_id" => $user_id,
),
),
);
if ($port != NULL) {
$data["port"] = array($port);
}
if ($encoding != NULL) {
$data["encoding"] = $encoding;
}
return $data;
}
what are the possible ways to write the above code in java
Don't look for direct, native support of translating json straight into Java objects, it needs to be done through libraries.
There are a (quite big) number of json libraries - GSON is one that I used, java EE json support is another. Search for others using your preferred search engine.
Use Jackson library to parse JSON to Java object.
I am trying to pass selected id to js on click of button.
My code is like this:
array(
'class'=>'CButtonColumn',
'template'=>'{email}',
'buttons'=>array
(
'email' => array
(
'label'=>'Send an e-mail to this user',
'click'=>'function($data->userId){
var y=confirm("Are you sure to send mail to this user?");
if(y==true){
window.location="todoList/sendEmail/$data->userId";
}
}',
),
),
),
Here i am not able to get userid inside js.
What can i do here?
Ok i found out answer to my question myself.My working code is:
array(
'class'=>'CButtonColumn',
'template'=>'{email}',
'buttons'=>array
( 'email' => array
(
'label'=>'Send an e-mail to this user',
'click'=>'function(){
return confirm("Are you sure to send mail to this user?");
}',
'url'=>'Yii::app()->createUrl("todoList/sendEmail", array("id"=>$data->userId))',
),
),
),