Groovy: convert url encoded to JSON - json

I have a string from encoded url like this:
leads%5Bupdate%5D%5B0%5D%5Bid%5D=123432456&leads%5Bupdate%5D%5B0%5D%5Bname%5D=%D0%A1%D0%B4%D0%B5%D0%BB%D0%BA%D0%B0+2342345345345&leads%5Bupdate%5D%5B0%5D%5Bstatus_id%5D=45645676&leads%5Bupdate%5D%5B0%5D%5Bprice%5D=0&leads%5Bupdate%5D%5B0%5D%5Bresponsible_user_id%5D=2423432&leads%5Bupdate%5D%5B0%5D%5Blast_modified%5D=1231423556&leads%5Bupdate%5D%5B0%5D%5Bmodified_user_id%5D=6756756&leads%5Bupdate%5D%5B0%5D%5Bcreated_user_id%5D=2423432&leads%5Bupdate%5D%5B0%5D%5Bdate_create%5D=1234534678&leads%5Bupdate%5D%5B0%5D%5Bpipeline_id%5D=4564564&leads%5Bupdate%5D%5B0%5D%5Baccount_id%5D=34578987&leads%5Bupdate%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bid%5D=567834&leads%5Bupdate%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bname%5D=MANGO+OFFICE+%D0%9D%D0%BE%D0%BC%D0%B5%D1%80+%D0%BB%D0%B8%D0%BD%D0%B8%D0%B8&leads%5Bupdate%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bvalues%5D%5B0%5D%5Bvalue%5D=74994567456&leads%5Bupdate%5D%5B0%5D%5Bcreated_at%5D=1234534678&leads%5Bupdate%5D%5B0%5D%5Bupdated_at%5D=1231423556&account%5Bsubdomain%5D=google&account%5Bid%5D=34578987&account%5B_links%5D%5Bself%5D=https%3A%2F%2Fgoogle.site.com
decoded string:
leads[update][0][id]=123432456&leads[update][0][name]=Сделка+2342345345345&leads[update][0][status_id]=45645676&leads[update][0][price]=0&leads[update][0][responsible_user_id]=2423432&leads[update][0][last_modified]=1231423556&leads[update][0][modified_user_id]=6756756&leads[update][0][created_user_id]=2423432&leads[update][0][date_create]=1234534678&leads[update][0][pipeline_id]=4564564&leads[update][0][account_id]=34578987&leads[update][0][custom_fields][0][id]=567834&leads[update][0][custom_fields][0][name]=MANGO+OFFICE+Номер+линии&leads[update][0][custom_fields][0][values][0][value]=74994567456&leads[update][0][created_at]=1234534678&leads[update][0][updated_at]=1231423556&account[subdomain]=google&account[id]=34578987&account[_links][self]=https://google.site.com
array:
[
'leads' => [
'update' => [
[
'id' => 22652446,
'name' => 'Сделка 74956462949',
'status_id' => 33687064,
'price' => 0,
'responsible_user_id' => 5827756,
'last_modified' => 1675346949,
'modified_user_id' => 8775297,
'created_user_id' => 5827756,
'date_create' => 1640068981,
'pipeline_id' => 3365869,
'account_id' => 28791871,
'custom_fields' => [
[
'id' => 662156,
'name' => 'MANGO OFFICE Номер линии',
'values' => [
[
'value' => 74994551765,
],
],
],
],
'created_at' => 1640068981,
'updated_at' => 1675346949,
],
],
],
'account' => [
'subdomain' => 'google',
'id' => 28791871,
'_links' => [
'self' => 'https://google.site.com'
],
],
]
and JSON:
{
"leads":
{
"update": [
{
"id":22652446,
"name":"\u0421\u0434\u0435\u043b\u043a\u0430 74956462949",
"status_id":33687064,
"price":0,
"responsible_user_id":5827756,
"last_modified":1675346949,
"modified_user_id":8775297,
"created_user_id":5827756,
"date_create":1640068981,
"pipeline_id":3365869,
"account_id":28791871,
"custom_fields": [
{
"id":662156,
"name":"MANGO OFFICE \u041d\u043e\u043c\u0435\u0440 \u043b\u0438\u043d\u0438\u0438",
"values": [
{
"value":74994551765
}
]
}
],
"created_at":1640068981,
"updated_at":1675346949
}
]
},
"account":
{
"subdomain":"google",
"id":28791871,
"_links":
{
"self":"https:\/\/google.site.com"
}
}
}
How I can convert it to JSON with Groovy?
Thanks.
P.S: I read this Convert url encoded data to json but it's not help.

Related

How to fix "_geoip_lookup_failure" tag with logstash filter "geoip" when posting a json with http to logstash

I am posting a json from an application to logstash wanting to get the location of an IP-adress with logstashes geoip plugin. However i get a _geoip_lookup_failure.
this is my logstash config
http {
port => "4200"
codec => json
}
}
filter{
geoip {
source => "clientip"
}
}
output {
elasticsearch { hosts => ["localhost:9200"] }
stdout { codec => rubydebug }
}
this is what I post to the port :
{'used_credentials': [
{'username': 'l', 'number_of_usages': 1, 'used_commands': {},
'get_access': 'false',
'timestamps': {'1': '04/15/2019, 21:08:54'}, 'password': 'l'}],
'clientip': '192.168.xxx.xx',
'unsuccessfull_logins': 1}
and this is what i get in logstash:
{
"unsuccessfull_logins" => 1,
"#version" => "1",
"used_credentials" => [
[0] {
"username" => "l",
"used_commands" => {},
"password" => "l",
"timestamps" => {
"1" => "04/15/2019, 21:08:54"
},
"number_of_usages" => 1,
"get_access" => "false"
}
],
"clientip" => "192.168.xxx.xx",
"#timestamp" => 2019-04-15T19:08:57.147Z,
"host" => "127.0.0.1",
"headers" => {
"request_path" => "/telnet",
"connection" => "keep-alive",
"accept_encoding" => "gzip, deflate",
"http_version" => "HTTP/1.1",
"content_length" => "227",
"http_user_agent" => "python-requests/2.21.0",
"request_method" => "POST",
"http_accept" => "*/*",
"content_type" => "application/json",
"http_host" => "127.0.0.1:4200"
},
"geoip" => {},
"tags" => [
[0] "_geoip_lookup_failure"
]
}
I don't understand why the input is recognized corectly but goeip does not find it
The problem is that your clientip is in the 192.168.0.0/16 network, which is a private network reserved for local use only, it is not present on the database used by the geoip filter.
The geoip filter will only work with public IP addresses.

How access array inside of JSON?

I'm use Guzzle for Laravel to send POST for Webservice, but I can't access array to send picture. That is the structed to I need follow:
{
"title": "xxxxxxxxxxxxxxxxx",
"category_id": "XXXX",
"official_store_id": null,
"pictures": [
{
"id": "XXXXX",
"url": "http://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"secure_url": "https://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"size": "500x500",
"max_size": "500x500",
"quality": ""
}
],
}
I try to send it like this:
$r = $client->request('POST', 'https://api.mercadolibre.com/items?access_token='.$token->erp_access_token, [
'json' => [
'title' => $name,
'category_id' => $cat2,
'price' => $price,
'currency_id' => 'BRL',
'available_quantity' => $quantity,
'buying_mode' => 'buy_it_now',
'listing_type_id' => 'gold_special',
'condition' => 'new',
'description' => $description,
'pictures' => [
'url' => $image
]
]
]);
Returns the error:
{"message":"body.invalid_field_types","error":"[invalid property type: [pictures] expected List but was JSONObject value (truncated...)
I read the Guzzle docs, but I not found any example for that case.
Any suggestion?
Change your pictures value from this
'pictures' => [
'url' => $image
]
to this
'pictures' => [
['url' => $image]
]

ZF3 zend-mvc generic route for many controllers in one module not working

I'm in ZF3, using the zend-mvc-skeleton and trying to configure a generic route that will match as many URLs as possible as I want to be able to create new controllers (including action methods of course), and have them immediately available.
The common approach described in the documentation is to write a route that matches the controller and action (same with ZF2).
Here is my module.config.php
namespace Application;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'default' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/:controller[/:action]]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
],
],
],
],
'controllers' => [/* ... */],
'view_manager' => [/* ... */],
],
It works like a charm for http://localhost/ and http://localhost/application calling the indexAction() function of the IndexController class inside the /module/Application/src/IndexController.php file.
However, it's not working when I try to get the fooAction() function in the same Controller (i.e. IndexController). It's not resolving correctly http://localhost/application/foo. and I get the following error:
A 404 error occurred
Page not found.
The requested controller could not be mapped to an existing controller class.
Controller:
foo (resolves to invalid controller class or alias: foo)
No Exception available
Same error if I try http://localhost/bar/foo to get the fooAction() in the barController.
Do you have any idea of what's wrong with this? Any help will be appreciated. Many thanks.
The route http://localhost/application/foo won't resolve to fooAction() in the index controller, since /foo in the URL will match the controller not the action. With that route setup you would need to visit http://localhost/application/index/foo.
To get it working you'll also need to make sure you have aliased your controller in the config, e.g. assuming you have:
'controllers' => [
'invokables' => [
'Application\Controller\Index' => \Application\Controller\IndexController::class
]
],
Then alias the controller so it matches the route parameter:
'controllers' => [
'invokables' => [
'Application\Controller\Index' => \Application\Controller\IndexController::class
],
'aliases' => [
'index' => 'Application\Controller\Index'
]
],
You'll need to add aliases that match the route parameter for each controller that isn't registered using the string you want for the route, e.g. a controller Namespace\Controller\BarController should be aliased to bar, etc.
I came here with similar problem. I have created two controllers in
"Application" module, and two in new module "Account" with the same name.
Application/Controller/IndexController
Application/Controller/OverviewController
Account/Controller/IndexController
Account/Controller/OverviewController
here are my modules.config.php
module/Account/config/module.config.php
return [
'router' => [
'routes' => [
'Account-account' => [
'type' => Segment::class,
'options' => [
'route' => '/account[/][:controller[/][:action][/]]',
'defaults' => [
'__NAMESPACE__' => 'Account\Controller',
'controller' => Account\Controller\IndexController::class,
'action' => 'index',
'locale' => 'en_us'
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard'
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => AccountControllerFactory::class,
Controller\OverviewController::class => AccountControllerFactory::class,
],
'aliases' => [
'index' => IndexController::class,
'overview' => OverviewController::class
]
],
and my
module/Application/config/module.config.php
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'Application-application' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/][:controller[/][:action][/]]',
'defaults' => [
'__NAMESPACE__' => 'Application\Controller',
'controller' => Application\Controller\IndexController::class,
'action' => 'index',
'locale' => 'en_US'
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard'
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => IndexControllerFactory::class,
Controller\OverviewController::class => IndexControllerFactory::class,
],
'aliases' => [
'index' => IndexController::class,
'overview' => OverviewController::class,
]
],
With this configuration if aliases sections are commented there is a error message which says that there is invalid controller or alias (index/overview).
If there are aliases
route: "application/overview/index" goes into Account module.

Yii2 RESTFUL API without models: getting 404 response

I'm implementing RESTFUL API with Yii2. The thing is I don't use models at all. My API just gets the request and performs some calculations (or passes request as a proxy to somewhere else) and outputs the response.
I have following controllers in app\modules\api\v1\controllers:
namespace app\modules\api\v1\controllers;
class CarController extends \yii\rest\Controller
{
// GET /v1/car/:id
public function actionIndex()
{
$carId = \Yii::$app->request->get('id');
// Forwarding request to other API
return ['some_reponse'];
}
// DELETE /v1/car/:id
public function actionDelete()
{
$carId = \Yii::$app->request->get('id');
// Forwarding request to other API
return ['some_reponse'];
}
public function behaviors()
{
return [
'verbs' => [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
'create' => ['post'],
'delete' => ['delete']
],
],
];
}
}
// ===============================
namespace app\modules\api\v1\controllers;
class CarsController extends \yii\rest\Controller
{
// POST /v1/cars
public function actionCreate()
{
$carData = \Yii::$app->request->post;
// Forwarding data to other API
return ['some_reponse'];
}
public function behaviors()
{
return [
'verbs' => [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
'create' => ['post']
],
],
];
}
}
// ===============================
namespace app\modules\api\v1\controllers;
class NotificationsController extends \yii\rest\Controller
{
// POST /v1/notifications
public function actionCreate()
{
$noticicationsData = \Yii::$app->request->post;
// Perform some additinal actions here
return ['some_reponse'];
}
public function behaviors()
{
return [
'verbs' => [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
'create' => ['post']
],
],
];
}
}
Url manager configuration:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => [ 'v1/cars' ],
'extraPatterns' => [ 'POST cars' => 'create' ]
],
[
'class' => 'yii\rest\UrlRule',
'controller' => [ 'v1/car' ],
'extraPatterns' => [ 'GET car' => 'index', 'DELETE car' => 'delete' ]
],
[
'class' => 'yii\rest\UrlRule',
'controller' => [ 'v1/notifications' ],
'extraPatterns' => [ 'POST notifications' => 'create' ]
]
]
]
Cars endpoint works fine. But other endoints return 404 error. Error response example:
{
"name": "Not Found",
"message": "Page not found.",
"code": 0,
"status": 404,
"type": "yii\web\NotFoundHttpException",
"previous": {
"name": "Invalid Route",
"message": "Unable to resolve the request: v1/car/options",
"code": 0,
"type": "yii\base\InvalidRouteException"
}
}
Any ideas whats wrong here? I guess something is wrong with my rules.
The problem was with urlManager rules configuration. For e.g. if I create CarController it treats like CarsController. So I have to set pluralize to false. Also modified extraPatterns section as well:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => [ 'v1/cars' ],
'extraPatterns' => [ 'POST' => 'create' ],
],
[
'class' => 'yii\rest\UrlRule',
'controller' => [ 'v1/car' ],
'extraPatterns' => [ 'GET' => 'index', 'DELETE' => 'delete' ],
'pluralize' => false
],
[
'class' => 'yii\rest\UrlRule',
'controller' => [ 'v1/notifications' ],
'extraPatterns' => [ 'POST' => 'create' ]
]
]
]
Problem with yii\rest\Controller::actions(), this method already have actions index , options,view etc. You need disabled action to use your action:
public function actions()
{
$actions = parent::actions();
unset($actions['view'], $actions['index']);
return $actions;
}

Not a HASH reference error received while trying to loop through array inside a hash

I've been trying to make a reddit bot which scrapes the moderator list from a given array of subreddits. So far I've only tried to use the reddit API to retrieve one single mod list (in JSON format). I've decoded it using the JSON perl module and now I'm trying to loop through it to build a mod list. Here's my code:
use strict;
use warnings;
use LWP;
use JSON;
use Data::Dumper;
my $ua = LWP::UserAgent->new('RedditBot/ModScraper');
my $req = HTTP::Request->new(GET => 'http://www.reddit.com/r/funny/about/moderators.json');
my $res = $ua->request($req);
if($res->is_success){
my $djson = decode_json($res->content);
my #datalist = $djson->{'data'}{'children'};
my #names;
for(my $i=0;$i<#datalist;$i++){
push(#names, $datalist[$i]->{'name'});
}
print Dumper #names;
}
else{
print $res->status_line, "\n";
}
And here is the Dumped hash/array:
$VAR1 = [
{
'mod_permissions' => [
'all'
],
'date' => '1201242956',
'name' => 'illuminatedwax',
'id' => 't2_14nvj'
},
{
'mod_permissions' => [
'all'
],
'date' => '1282272786',
'name' => 'krispykrackers',
'id' => 't2_35gvu'
},
{
'mod_permissions' => [
'all'
],
'date' => '1306697593',
'name' => 'doug3465',
'id' => 't2_4d9s0'
},
{
'mod_permissions' => [
'all'
],
'date' => '1306884548',
'name' => 'Kylde',
'id' => 't2_q22x'
},
{
'mod_permissions' => [
'all'
],
'date' => '1318239301',
'name' => 'GuitarFreak027',
'id' => 't2_3uh1q'
},
{
'mod_permissions' => [
'all'
],
'date' => '1328330491',
'name' => 'sodypop',
'id' => 't2_39hzo'
},
{
'mod_permissions' => [
'all'
],
'date' => '1334668866',
'name' => 'brownboy13',
'id' => 't2_4itnj'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1338375544',
'name' => 'neptath',
'id' => 't2_42sls'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1338375575',
'name' => 'kabuliwallah',
'id' => 't2_3wqp9'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1338375584',
'name' => 'poop_monster',
'id' => 't2_45a14'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1342052024',
'name' => 'WellEndowedMod',
'id' => 't2_66grn'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1351424249',
'name' => 'complex_reduction',
'id' => 't2_4y36s'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1351427857',
'name' => 'anions',
'id' => 't2_33ggu'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1363446597',
'name' => 'Livyka',
'id' => 't2_5p6ae'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1363450026',
'name' => 'Lord_Nuke',
'id' => 't2_4fvxr'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1363450241',
'name' => 'redtaboo',
'id' => 't2_3belm'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1363453699',
'name' => 'Knowltey',
'id' => 't2_3aadp'
},
{
'mod_permissions' => [
'wiki',
'posts',
'flair'
],
'date' => '1401789143',
'name' => 'AutoModerator',
'id' => 't2_6l4z3'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789148',
'name' => 'thejellydude',
'id' => 't2_3g59f'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'config',
'flair'
],
'date' => '1401789172',
'name' => 'DuckDragon',
'id' => 't2_52snu'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789188',
'name' => 'Kensin',
'id' => 't2_4nbrl'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789200',
'name' => 'cahman',
'id' => 't2_6y7ld'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789214',
'name' => 'topodan',
'id' => 't2_8nisz'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789225',
'name' => 'ADHD_orc',
'id' => 't2_5dwik'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789236',
'name' => 'all_my_rage',
'id' => 't2_6z81j'
},
{
'mod_permissions' => [
'mail'
],
'date' => '1401789251',
'name' => 'funny_mod',
'id' => 't2_5qz0k'
},
{
'mod_permissions' => [
'wiki',
'posts',
'mail',
'flair'
],
'date' => '1401789269',
'name' => 'benologist',
'id' => 't2_358ts'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789281',
'name' => 'dizzie131',
'id' => 't2_bb3q5'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789298',
'name' => 'KimJongUnchained',
'id' => 't2_b9c4n'
}
];
My error is:
Not a HASH reference at modscraper.pl line 15.
I know this is probably something trivial, but I'm a pretty new Perl programmer and haven't done much JSON/Networking work before. Thanks for reading.
EDIT: Forgot to mention, line 15 is the for loop and the dumped array/hash is #datalist.
As the dumped hash/array value that you have given is a array reference, so the datalist variable should be defined as a array ref, so the code line for that should be changed to:
my $datalist = $djson->{'data'}{'children'};
also, we have to change the for loop code because now datalist is a array ref, so you can change the for loop code to this:
for(my $i=0;$i<#$datalist;$i++){
push(#names, $datalist->[$i]->{'name'});
}