URL encoding giving errors when contacting API - javascript

I'm working on a school project where I'm trying to make a search function for The New York Times Article Search API.
I have a problem with one of their search filters as when I run the function I get error 400 as for some reason it can't read the url encoding. The fun thing is if I replace all the %3D's with = and the %26's with & in my own URL it works and I can see the API responds correctly.
if($('#date1').is(':checked')) {date="day_of_week&begin_date=18500101&end_date=19000101";}
if($('#date2').is(':checked')) {date="day_of_week&begin_date=19000101&end_date=19500101";}
if($('#date3').is(':checked')) {date="day_of_week&begin_date=19500101&end_date=20000101";}
if($('#date4').is(':checked')) {date="day_of_week&begin_date=20000101&end_date=20150101";}
$.getJSON('http://api.nytimes.com/svc/search/v2/articlesearch.json',
{'api-key': 'XXXXXXXXXXXXXXXXXXXXXXX',
'fq': 'headline:("'+sogestreng.toLowerCase()+'")'+" AND "+finalSections,
'facet_field': date},
This code returns "http://api.nytimes.com/svc/search/v2/articlesearch.json?api-key=XXXXXXXXXXXXXXXXXXXXXXX&fq=headline%3A(%22dubai%22)+AND+section_name.contains%3A(%22Sports%2C+%22)&facet_field=day_of_week%26begin_date%3D20000101%26end_date%3D20150101"
While if I manually replace the last part of the url encoding and open it in my browser I get the result I'm looking for.
I do this by changing:
"&facet_field=day_of_week%26begin_date%3D20000101%26end_date%3D20150101"
to
"&facet_field=day_of_week&begin_date=20000101&end_date=20150101"
Also to clarify, the "fq" criteria works perfectly fine, it's just the facet_field.
How can this be? And is there any fix for it?

You're shoving the description of several fields into one field's value. To get the results you want, try:
var begin_date, end_date;
if($('#date1').is(':checked')) {
begin_date = 18500101;
end_date = 19000101;
}
else if($('#date2').is(':checked')) {
// etc.
}
$.getJSON('http://api.nytimes.com/svc/search/v2/articlesearch.json',
{
'api-key': 'XXXXXXXXXXXXXXXXXXXXXXX',
'fq': 'headline:("'+sogestreng.toLowerCase()+'")'+" AND "+finalSections,
'facet_field': 'day_of_week',
'begin_date': begin_date,
'end_date': end_date
},
// ...

Related

Send signature png to PDFotter api

I've got this great API for filling pdfs that's called pdfotter, I'm looking for someone who used this API before and maybe found a way to fill in a signature in the form.
The signature part is very important and can not be replaced with a simple string for legal reasons.
My request looks like this:
let form = {
signature: fs.createReadStream('1.png'),
date: new Date().toLocaleDateString('en-gb'), // default
};
request
.post('https://www.pdfotter.com/api/v1/pdf_templates/.../fill')
.form({
data: {
...form
},
})
.auth('...', '')
.pipe(fs.createWriteStream('result.pdf'));
The result in the signature looks like this:
#ActionController::Paramet ers:0x0000000674cf60>
Any suggestions are welcome!
I am not familiar with this PDFotter service, but anyway I see that your request is formed incorrectly: form.signature field is not filled with a suitable value, because you set a stream there.
signature: fs.createReadStream('1.png') <- this will not work in any way.
Try to replace signature with some valid value, for example a string Michael Ionov.
let form = {
signature: `Michael Ionov`,
date: new Date().toLocaleDateString('en-gb'), // default
};
I understand that you probably need to paste an image there, but firstly check if it works with 100% valid value.
There is no information on pdfotter web site about sending images, but if you are sure they support it - you will need to try pass to signature raw image data using for example fs.readFileSync('img.png'), or Base64 encoded image:
signature: new Buffer(fs.readFileSync('img.png').toString('base64');
So the API providers answered me and there is actually a nice way to do it but is not yet documented, all I needed to do is to prepend 'image:' to the signature field and also prepend it on the form itself in their, and send the image as a base64 string.
'image:signature': fs.readFileSync('signature.png').toString('base64')
Happy hacking!

Searching data using Youtube API

Extremely new at this, but wanted to try to build something. Decided on a simple landing page that would assist in pulling Youtube videos. Got my API key and got to building. Ran into a few issues. Found out that it can't call from a local HTML file, so worked out another way to test, but now I have another issue.
I have the following code set to run when a button is pressed, submitting the text in form as the variable topic.
function search() {
var hdi = "how do i ";
var request = gapi.client.youtube.search.list({
part: 'id',
q: hdi + topic,
});
request.execute(onSearchResponse);
};
function onSearchResponse(response){
var responseString = JSON.stringify(response, " ", 2);
console.log(responseString);
}
But I cannot seem to get the response I'm looking for. The JSON data I get seems to correspond to medley of search engines (Yahoo, duck duck go, ect) instead, not the JSON data for the YouTube search.
Here is a CodePen, if that assists more. I have some code commented out that I'm waiting to work on until I get this part implemented. Thanks for any help you can give me!
https://codepen.io/billsobill/pen/OvxNON

Codebird.js for Twitter API won't return more than one tweet in single call

Codebird.js is not working when I try to return a list of n number of tweets by adding to my params object.
It works when I include just the property screen_name to get a single tweet but when I add count in, as below, the response I get is still for only one tweet
params = {
"screen_name": screenName,
"count": "3"
};
I can't seem to find any codebird.js documentation besides the README.MD on the main github page.
Is my syntax correct? Am I approaching this the correct way by adding to params
Solved it. Turns out I was using the wrong api endpoint and should have been using statuses/user_timeline.
Note: Check this part of the docs to see how to map this endpoint's string to the right format https://github.com/jublonet/codebird-js#mapping-api-methods-to-codebird-function-calls

JSON spreadsheet returns null, or am I an idiot

So, have been trying to get a javascript to read a spreadsheet, but needed it private rather than public, so spent quite a bit of time trying to figure out how to do that. It is actually quite easy, or seems like it is, except that I still can't read the spreadsheet. Will cover private spreadsheets through the cloud console in the end.
So, I have a rather simple spreadsheet which contains about 6 columns, (including customer emails and similar, hence why private). And I have been playing about trying to get it to work for the last couple of days.
First, the string!
So to enable this I first need a string that will return a json object (is that what I am expecting back?)
https address: https://spreadsheets.google.com/feeds/list/
Sheet ID :
Additional : /od6/private/full
Command : ?alt=json-in-script&callback=importGSS
or full string : https://spreadsheets.google.com/feeds/list//od6/private/full?alt=json-in-script&callback=importGSS
This seems to work quite fine, if I remove the json part it shows me the data sheet, if I ad it, I get an empty file instead.
So now to the code.
The function that SHOULD be returning the object is the following (spLink) contains the link above.
function loadData(spLink){
$.getJSON(spLink).always(function(data) {
console.log("Object created!");
console.log(data);
}).fail(function(message) {
console.error('Something went pretty wrong!');
console.error(message);
}).done(function(){
console.log('Done!');
});
}
The only thing I can really tell about it is that data continues returning null, and I can't really figure out where else the data may end up.
Have tried both with and without callback.
The list feed only works if there are no empty cells in a row with data, among other pitfalls.
Use the cell feed instead, which requires more code because you wont get the data organized by rows.

Script error: "Unable to get value of the property 'split': Object is null or undefined

I searched around, and couldn't find an answer to my question. I'm very new at coding, and at work, we have an application that current names that are logged in, and what they are doing.
Recently, they have changed from jquery 1.4.1 to jquery 1.8.3. Ever since then, I cannot get the results to process correctly, because of the following error;
"Unable to get value of the property 'split': Object is null or undefined"
I have the code setup to grab the results and split them;
function processAgents(xData, status) {
var avail = xData.responseText.split("|")[0];
var acw = xData.responseText.split("|")[1];
var total = xData.responseText.split("|")[2];
var breaks = xData.responseText.split("|")[3];
var pending = xData.responseText.split("|")[4];
The application is setup to open as an HTA file which opens up the PHP script.
Any help would be appreciated, please let me know if I left anything out!
Thanks!
EDIT 1
I did some more investigating, and it looks like I'm not getting data from my process request. This is how it is currently setup
function updateAgents() {
var ts1 = new Date().getTime();
$.ajax({
url: "http://SERVER/AgentSrc.php?x=" + ts1,
complete: processAgents
I'm not sure if this is processing correctly since they went to jquery 1.8.3.
EDIT 2
So after looking into it more, it doesn't appear that the script is getting the data from the server, even though I have access. If I make a local file and put the information in it, it will pull the information and split it, but if I point to the path of the file on the server, it won't get the information. But the strange thing is, if I run it using jquery 1.4.1, it pulls the data fine, but can't display it. But with 1.8.3, it doesn't allow me to pull it from the server.
thanks again!
This will give some clarity
xData.responseText.toString().split("|")[0];
(split is part of string not jQuery)
Here is a possible explanation: in earlier versions of jQuery, ajax calls returned an xmlHttpRequest (XHR) object. Recent versions return a promise (jqXHR) instead.
See this page for more details.

Categories