Batch request with fql using javascript - javascript

The following batch request for retrieve friends using the same app is not working:
var search = {batch: [
{
'name' : 'getFriends',
'relative_url': 'method/fql.query?query=SELECT+uid,+first_name,+name,+pic_square,+pic_small+FROM+user+WHERE+is_app_user=1+and+uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1=' + userId + ')'
},
{
'method': 'get',
'relative_url': '{result=getFriends:$.data[*].uid}/news.reads/article',
}
]};
and the following code executes the batch request:
FB.api('/', 'post', search, function(response) {
console.log(response);
});
but the first search returns null. What's wrong with the query?
According to facebook documentation for batch requests, the *relative_url* is
(...) a relative_url (the portion of the URL after graph.facebook.com)
get from here.
So I changed the first block of code to:
var search = {batch: [
{
'name' : 'getFriends',
'method' : 'GET',
'relative_url': 'method/fql?q=SELECT uid, first_name, name, pic_square, pic_small FROM user WHERE is_app_user=1 and uid IN (SELECT uid2 FROM friend WHERE uid1=me())'
},
{
'method': 'GET',
'relative_url': '{result=getAmigos:$.data[*].uid}/news.reads/article'
}
]};
the relative_url of the first block on the batch works at Open Graph API Tool at facebook, but at the JS the http code is 500 and the message body is:
{
"error": {
"message": "Graph batch API does not allow REST API method fql",
"type": "FacebookApiException",
"code": 3
}
}
but in the api batch requests documentation allows fql queries. The documentation is outdated? Should I open a bug?
P.S: the fql without the batch request is
SELECT uid, first_name, name, pic_square, pic_small
FROM user WHERE is_app_user=1
and uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

Don't use method/fql; Facebook's FQL endpoint is now just fql.
...
"name": "getFriends",
"method": "GET",
"relative_url": "fql?q=SELECT...",
...

it's working now. I just add the access_token parameter at the batch object. like this:
var searchArgs = {
access_token: FB.getAuthResponse().access_token,
batch: []
};
searchArgs.batch.push({
'method': 'GET',
'name': 'amigosapp',
'relative_url': 'method/fql.query?query=SELECT+uid,+first_name,+name,+pic_square,+pic_small+FROM+user+WHERE+is_app_user=1+and+uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1=me())&limit=20',
'omit_response_on_success': false
});
searchArgs.batch.push({
'method': 'GET',
'relative_url': 'news.reads/article?date_format=U&ids={result=amigosapp:$.[*].uid}'
});
the other trick was use the jsonpath expression in a different way. I couldn't make the second request for each user of the first request. So I changed the way to get it adding the friends ids at the end of the graph api request. I think that my first thought about a request per friend it's not possible using the batch request.
Then I did the call of the batch:
FB.api('/', 'POST', searchArgs,
function(response) {
var friends = response[0].body;
console.log(friends);
var activities = response[1].body;
console.log(activities);
}
);
Now it's just organize the posts retrieved.
[]'s

Related

Axios request to Opensearch/Elasticsearch

Currently able to GET and POST to my collection but need the ability for more complicated queries, I am using bodybuilder to structure the request and axios as my client.
However using POST doesn't return my specified results instead just the first 10 items in my index and using GET I'm unable to send a body for these complicated requests leaving me with POST.
I've switched setting my data from data:data to body:data with the same result.
Currently this is my POST which again returns data but NOT my filtered data just the first 10 items of my collection.
Any insight would be appreciated!
export function searchQuery(search: string) {
var body = bodybuilder().query("query_string", "query", search).build();
const data = JSON.stringify(body);
axios({
url: `${SEARCH_URL}`,
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: data,
}).then((res) => {
console.log(res);
});
}
This is my current log of data:
{"query":{"query_string":{"query":"Green"}}}
Based on the comments in the question above, you are getting only the first 10 items in your collection, when you run the query against elasticsearch directly.
This is because elasticsearch by default returns only 10 documents in the search result if no size param is included in the search query.
If you want to get more than 10 results, you need to modify your search query as
{
"size": 20, // change this according to your requirement
"query": {
"query_string": {
"query": "Green"
}
}
}

Jquery ajax POST inconsistent with Hapijs server inject

Here is what my Ajax post looks like:
$.ajax({
type: "POST",
url: "/create",
data: {
"question": $('#question').val(),
"options": options
},
success: function() { window.location.href="/view"; }
});
Fairly simple. options is an array of string charachters. The problem is, when I receive on the server end with Hapijs, the request payload shows this object received:
{
question: "..etc..",
"options[]": [..etc...]
}
Why does it add a [] to the options variable name? Normally this wouldn't be a problem for me, but when I do the same thing and simulate a server request in my lab test like this:
var test = [..etc..]
// Simulate POST request
var serverOptions = {
method: 'POST',
url: '/create',
payload: {
question: 'Question',
options: test
}
};
It shows that the variable name received is just "options", not "options[]". How can I get jquery to stop adding the [] to the variable name when POSTing? Thanks

GitHub API results different between cURL and jQuery AJAX

I'm trying to query GitHub's search user API by language and location. When I use the command curl https://api.github.com/search/users?q=location:denver+language:php I receive 146 results. However, when I try to use jQuery AJAX, I receive an error.
My JavaScript code:
var url = "https://api.github.com/search/users";
var request = {
q: "location:denver+language:php",
sort: "repositories",
order: "desc"
};
var result = $.ajax({
url: url,
data: request,
dataType: 'jsonp',
type: 'GET'
})
.done(function() {
alert(JSON.stringify(result));
})
.fail(function() {
alert('failed');
});
What alert(JSON.stringify(result)); gives:
{
"readyState":4,
"responseJSON":{
"meta":{
"X-RateLimit-Limit":"10",
"X-RateLimit-Remaining":"9",
"X-RateLimit-Reset":"1397393691",
"X-GitHub-Media-Type":"github.beta",
"status":422
},
"data":{
"message":"Validation Failed",
"documentation_url":"https://developer.github.com/v3/search/",
"errors":[
{
"message":"None of the search qualifiers apply to this search type.",
"resource":"Search",
"field":"q",
"code":"invalid"
}
]
}
},
"status":200,
"statusText":"success"
}
When I only use one qualifier on q it works fine and the result.responseJSON.data object contains the information that is normally provided by cURL.
use a space character instead of a plus(+). Change your query to this:
q: "location:denver language:php",
and it will work as expected, because jquery will convert this space character to a plus.

how to post object to facebook user wall?

I am using Facebook JavaScript SDK, this code is for log in and post an object to user's wall, which returns an error,
{"error":{"message":"(#200) You do not have sufficient to permissions to perform this action","type":"OAuthException","code":200}}
FB.login(function(data){
if(data.authResponse){
facebookGetDetails();
}else{
console.log("Login operation aborted");
}
},{scope: 'email,publish_actions,read_stream'});
FB.api(
'/me',
'post',
{
app_id: xxxxxxxxxxxx,
type: "business.business",
url: url,
title: title,
image: picUrl,
contact_data: "Bangalore, India",
location: "17.024522",
description: desc
},
function(response) {
console.log(JSON.stringify(response));
// handle the response
});
the following code works, but it only posts to activity stream, not to wall.
FB.api(
'me/namespace:review',
'post',
{
business: "http://samples.ogp.me/616004095080596"
},
function(response) {
// handle the response
}
);
I guess you're using a incomplete endpoint URL here
FB.api(
'/me',
...
);
You should call /me/feed with a post request as described here: https://developers.facebook.com/docs/graph-api/reference/user/feed/#publish

how to call web service rest based using ajax + jquery

I am calling web service on this url .
here is my code .
http://jsfiddle.net/LsKbJ/8/
$(document).ready(function () {
//event handler for submit button
$("#btnSubmit").click(function () {
//collect userName and password entered by users
var userName = $("#username").val();
var password = $("#password").val();
//call the authenticate function
authenticate(userName, password);
});
});
//authenticate function to make ajax call
function authenticate(userName, password) {
$.ajax({
//the url where you want to sent the userName and password to
url: "",
type: "POST",
// dataType: 'jsonp',
async: false,
crossDomain: true,
contentType: 'application/json',
//json object to sent to the authentication url
data: JSON.stringify({
Ticket: 'Some ticket',
Data: {
Password: "1",
Username:"aa"
}
}),
success: function (t) {
alert(t+"df")
},
error:function(data){
alert(data+"dfdfd")
}
})
}
Response
**
**
It mean that I first call this method then call login method ?
Perhaps the message means that during development, while you are writing and testing the code, use the URL:
http://isuite.c-entron.de/CentronServiceAppleDemoIndia/REST/GetNewAuthentifikationTicket
rather than:
http://isuite.c-entron.de/CentronService/REST/Login
Because you don't need the application id for the development method. You can see from the error message that you are missing the application (gu)id
The guid '61136208-742B-44E4-B00D-C32ED26775A3' is no valid application guid
Your javascript needs to be updated to use new url http://isuite.c-entron.de/CentronServiceAppleDemoIndia/GetNewAuthentifikationTicket as per the backend sode team.
Also, even if you do this, your will not be able to get reply correctly since service requires cross domain configuration entries in web.config. You have to use this reference:http://encosia.com/using-cors-to-access-asp-net-services-across-domains/ and configure server's web.config in a way so that you can call it from cross domain.

Categories

Resources