I have built a web application that passes all form input data through the URL. Can someone help me figure out how to send data using POST instead? I am starting to handle large amounts of data, and GET creates an issue with the URL being too long. If there is a simple JavaScript way to do this, I'd be interested as well.
For example, the input form has an attribute called txtName and the user has typed John. On the new form, I need it to get John and assign it to the variable $Name using the POST method.
Right now I'm using GET and am chomping the parameters to get the data out of the URL.
How do you, in Perl, get POST to work on both forms?
I changed GET to POST on the form submitting data. On the form receiving data I put
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
at the top, then under that I put
my req = POST 'http://intranet/webservice/enhancements/ManagementEnhancementPerson.pl',
[ $Person ='person' ];
The web page is the page I'm taking data from, $Person is the variable I want it to be, and person is the name of the input on the original form.
I've been stuck on this for a while. I'm relatively new to Perl and web development.
Here are my use statements at the top of the page receiving the data.
use CGI::Carp 'fatalsToBrowser';
use warnings;
use Net::SMTP;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
If you make the request using the following (slightly altered from what you posted to make sense):
my $request = POST 'http://.../ManagementEnhancementPerson.pl', [
person => $Person,
];
my $response = $ua->request($request);
You would extract the value using the following in ManagementEnhancementPerson.pl:
use CGI qw( );
my $cgi = CGI->new;
my $Person = $cgi->param('person');
The CGI module provides the form data via param regardless of whether the form was submitted using GET or POST.
Related
Just want to ask if anyone can help convert this to powershell? I believe this is Python. I am trying since yesterday and couldnt get the exact output.
Below Codes is in Pyhton and I cant convert it on powershell. I tried searching ways on how to filter things using Restmethod, JSON API to powershell but I cant make it work.
url = "https://test/"
input_data = '{"list_info":{"row_count":999,"start_index":1,"sort_field":"created_time","sort_order":"desc","get_total_count":true,"search_fields":{"subject":"ALERT"},"filter_by":{"name":"35704_MyView"},"fields_required":["subject","created_time"]}}'
params = {
"TECHNICIAN_KEY": "ABCD-EDFC-QMFDJVA-PAOMD",
"OPERATION_NAME": "read",
"input_data": input_data
}
response = requests.get(url, params=params, verify=False)
resp_json = response.json()
Basically I need to connect using restmethod and filter those highlighted in yellow using powershell.
Thanks!
I suspect the Python is just taking a hash of params and building a querystring for you.
You could do this with some string interpolation in PowerShell.
$url = "https://test/"
$input_data = '{"list_info":{"row_count":999,"start_index":1,"sort_field":"created_time","sort_order":"desc","get_total_count":true,"search_fields":{"subject":"ALERT"},"filter_by":{"name":"35704_MyView"},"fields_required":["subject","created_time"]}}'
$technician_key = "ABCD-EDFC-QMFDJVA-PAOMD"
Invoke-RestMethod -Uri "$url`?TECHNICIAN_KEY=$technician_key&OPERATION_NAME=read&input_data=$input_data"
The Uri will end up looking like this:
https://test/?TECHNICIAN_KEY=ABCD-EDFC-QMFDJVA-PAOMD&OPERATION_NAME=read&input_data={"list_info":{"row_count":999,"start_index":1,"sort_field":"created_time","sort_order":"desc","get_total_count":true,"search_fields":{"subject":"ALERT"},"filter_by":{"name":"35704_MyView"},"fields_required":["subject","created_time"]}}
The backtick (`) in the string after $url tells the PowerShell parser that the question mark (?) is not part of the variable name.
There are some other ways to do this and I can't test this myself so some adjustments may be required for the service you're working with. As for the format of the input_data string, I can only guess. Hopefully, you can tweak as needed.
I have in a text database field a json encoded chart configuration in the form of:
{"Name":[[1,1],[1,2],[2,1]],"Name2":[[3,2]]}
The first number of these IDs is a primary key of another table. I'd like to remove those entries with a trigger when the row is deleted, a plperl function would be good except it does not preserve the order of the hash and the order is important in this project. What can I do (without changing the format of the json encoded config)? Note: the chart name can contain any characters so it's hard to do it with regex.
You need to use a streaming JSON decoder, such as JSON::Streaming::Reader. You could then store your JSON as an array of key/value pairs, instead of a hash.
The actual implementation of how you might use do this is highly dependent on the structure of your data, but given the simple example provided... here's a simple implementation.
use strict;
use warnings;
use JSON::Streaming::Reader;
use JSON 'to_json';
my $s = '{"Name":[[1,1],[1,2],[2,1]],"Name2":[[3,2]]}';
my $jsonr = JSON::Streaming::Reader->for_string($s);
my #data;
while (my $token = $jsonr->get_token) {
my ($key, $value) = #$token;
if ($key eq 'start_property') {
push #data, { $value => $jsonr->slurp };
}
}
print to_json(\#data);
The output for this script is always: -
[{"Name":[[1,1],[1,2],[2,1]]},{"Name2":[[3,2]]}]
Well, I managed to solve my problem, but it's not a general solution so it will probably not help the casual reader. Anyway I got the order of keys using the help of the database, I called my function like this:
SELECT remove_from_chart(
chart_config,
array(select * from json_object_keys(chart_config::json)),
id);
then I walked through the keys in the order of the second parameter and put the results in a new tied (IxHash) hash and json encoded it.
It's pretty sad that there is no perl json decoder that could preserve the key order when everything else I work with, at least on this project, does it (php, postgres, firefox, chrome).
JSON objects are unordered. You will have to encode the desired order into your data somehow
{"Name":[[1,1],[1,2],[2,1]],"Name2":[[3,2]], "__order__":["Name","Name2"]}
[{"Name":[[1,1],[1,2],[2,1]]},{"Name2":[[3,2]]}]
May be you want streaming decoder of JSON data like SAX parser. If so then see JSON::Streaming::Reader, or JSON::SL.
Situation is the I am dealing with REST and JSON. Making JSON request for a REST client.
I have a simple for loop that creates ids for me
for (my $i=1;$i<=2504;$i++)
{
push (#elements,$i);
}
my $ids = join ',',map{"\"$_\""}#elements;
However, when I pass this to JSON then I see backslash are being printed
$hashref=({"varreq"=>{"search"=>{"Ids"=>[$ids],"genome"=>[{"hugo"=>["$hugo"]}]},"output_format">{"groupby"=>"gene"}}});
Above is encoded in JSON and then a post request is made
I am getting this:
"\"1\",\"2\",\"3\",\"4\",......
and I want:
"1","2","3","4",.....
If you're doing JSON, why not just:
use JSON;
Rather than hacking it with regular expressions:
#!/usr/bin/env perl
use strict;
use warnings;
use JSON;
my $json_str = to_json ( [1..2504] );
print $json_str;
With to_json you can encode into a JSON structure pretty much any perl data structure. (and from_json to turn it back again).
You can do an OO style with encode_json/decode_json.
You seem to be doing this already, but ... this here:
{"Ids"=>[$ids],
Can be simply changed as the above:
{ "Ids" => [#elements]
Which should do what you want.
From the comments - I don't think anything receiving JSON should be getting confused by an array of numbers vs. an array of numeric strings.
But if they do:
my $json_str = to_json [ map {"$_"} 1..2504 ];
well, after encoding but before making the POST request I end up doing the following and it worked:
$postRequestJSON=~s/\\//g;
I haven't tried much like this before, so I want to know what is the recommended route for doing what I want to do.
I'm trying out the PunchFork.com api which returns recipes in a JSON dictionary. Here's an example.
How would I go about returning the "title", "thumb", and "pf_url" for each recipe so that I can display them on the page? Currently there is a couple of selects which values are used to create the url, but I don't know what to do with that url in order to display data on the page.
If you have any questions just ask.
What language are you using to generate the webpage? Most languages have built-in mechanisms for turning a JSON string into an object that the language understands.
Here's an example that uses PHP to generate an HTML page with all of the images from the API you referenced:
$response = file_get_contents($url);
$object = json_decode($response);
// Now $object is an object with the same data represented in the JSON string,
// and you can refer to its class members accordingly:
foreach ($object->recipes as $recipe)
{
$imageUrl = $recipe->source_img;
echo '<img src="$imageUrl" />';
}
As you all know there is an Export "Button" when you Click(in Drupal) on your View(e.g. Customer Data etc.). So I'd like to visualize somehow the data I geht here(which is unfortunately JSON Objects in PHP Style). Most Viewers & Readers (e.g. this here http://jsonviewer.stack.hu/ or some other SW solutions) can't read this Type of JSON!
I'm aware of the possibility of parsing this Exported JSON Data in php but I'm lookin for a more friendly solution.
Thanxx out there!
George
It's not 'PHP style JSON' (I'm quite sure such a thing doesn't exist) it's a PHP array!
Either grab the views datasource module which can export views as JSON/XML natively, or simply run the PHP output through json_encode().
If the output looks like this:
$view = array(
'something' => 'something',
...
);
Then you need to run $string = json_encode($view); echo $string;.
That will output the View as a JSON object/array :-)