Reputation: 1614
I am calling from an xml file, I am basically just trying to get the data out of the xml file into json, this is what i have
my $url ="someURL"
my $req = HTTP::Request->new(GET => $url);
$req->content_type('application/json');
my $json = new JSON;
my $request_json = $json->allow_blessed->encode($req);
$req->content($request_json);
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $response_json = $response->content;
my $parsed = $json->allow_nonref->utf8->relaxed->decode($response_json);
The last line gives the following response:
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "<mytag...")
Upvotes: 0
Views: 700
Reputation: 126732
Look at these lines of your code
my $req = HTTP::Request->new(GET => $url);
$req->content_type('application/json');
That's fine. You have created an HTTP::Request
object that makes a GET
request to the URL, and you have set a header in that request that says that the content will be JSON data. All that is necessary now is to add that JSON data.
Now this is very odd
my $json = new JSON;
my $request_json = $json->allow_blessed->encode($req);
$req->content($request_json);
You have encoded the whole of the HTTP::Request
object as JSON and set that as the object's content.
I hope you can see that that isn't the way it's supposed to work? An HTTP request isn't expected to send a copy of itself as the data content.
What you should be adding as content I can't tell as it's a property of the the site you're sending the request to. Normally it's a description of the query that you want to make, in this instance formulated as a JSON string.
If this doesn't help you enough then you must tell us much more about the site you're trying to query.
Upvotes: 1
Reputation: 385966
I am basically just trying to get the data out of the xml file into json
JSON is normally created a data structure made of arrays, hashes, strings and numbers. Once you have this, all you need to do is
my $json = encode_json($data);
So you need to build the correct data structure. Since you didn't tell us what that is, I presume you haven't even decided what it is yet. That's the first thing you must do.
Upvotes: 0