Angular JSONP request - json

I'm trying to get a jsonp response from an api call api.brewerydb.com. However, it's not wrapping the json with a function on its call back. Here is my code:
app.factory('beer', ['$http',function($http){
var url = "http://api.brewerydb.com/v2/beers?key=MYKEY&application/json&name=oberon&callback=JSON_CALLBACK";
$http.jsonp(url)
.success(function(data){
return data;
});
}]);
It is returning json data, however I get a syntax error at a file whose name is the url I passed and it contains the json data.
I discovered that the api does not support jsonp which is why this isn't working out for me

Your url may be incorrect: '../application/json..' is for an optional HTTP_ACCEPT header per their api docs here: breweryDB , so remove it from the var url string if its not needed.
Per their docs, the default return type is JSON, so no need to pass an extra parameter if its not needed.
Try this instead (for the endpoint /Beers) per their endpoint doc :
var url = "http://api.brewerydb.com/v2/beers?key=MYKEY&name=oberon&callback=JSON_CALLBACK";
Go get them beers!
EDIT: code is here: http://jsfiddle.net/r47y3mq3/1/ copy/paste to your local dev environment and use Safari. Using Chrome results in an Access-Control-Allow-Origin header concern as discussed here: access-control

I was having some similar issues. You have name=oberon which is a brewery, but you are searching for beers in your url.
If you tried:
var url = "http://api.brewerydb.com/v2/breweries?key=MYKEY&application/json&name=oberon&callback=JSON_CALLBACK";
...you'd get an array of all the beers in the Oberon brewery.

Related

"Invalid argument" when sending GET to spaces.members.list

I'm trying to create a Google Hangouts Chat chatbot (in G Suite) using Apps Script. I want to get a list of everyone in the chatroom, but this isn't directly supported in Apps Scripts yet, so I'm using the rest API. The API call list seems straightforward:
The command is
GET https://chat.googleapis.com/v1/{parent=spaces/*}/members
I've created a service account for authorization and then used
var endpoint = 'https://chat.googleapis.com/v1/{parent="spaces/pQkgxxxxxxx"}/members'
var options = {
method: "GET",
contentType : "application/json" ,
muteHttpExceptions : true,
headers: {
"Authorization": "Bearer " + goa.getToken(),
}
};
var response = UrlFetchApp.fetch(endpoint, options)`
To which I get
Invalid argument: https://chat.googleapis.com/v1/{parent="spaces/pQkgxxxxxxxx"}/members
I've tried encoding the parent parameter, but the error persists. Any ideas?
Per official documentation on the page you linked, the expected format of the path parameter parent is of the form spaces/*. The example value given is spaces/AAAAMpdlehY
In other words, you are not expected to write the {parents= and } bits, even though the template URL
GET https://chat.googleapis.com/v1/{parent=spaces/*}/members
has them. This template url format is explained in-depth on the Google API HTTP annotation website.
In your example, the correct URI to GET is https://chat.googleapis.com/v1/spaces/pQkgxxxxxxx/members
You should also consider that it may take multiple calls to resolve all members of the space pQkgxxxxxxx, by checking for a nextPageToken in the response (and passing that as the URL parameter pageToken in the next call).
You should also consider that the MemberShip returned by this query may include members with various states of membership.

weird file listing response differences between v2 and v3

I am using the google-drive-sdk with our company-made device. We upload pictures made by our device to google drive. After that I try to list the files with a GET request to https://www.googleapis.com/drive/v2/files to get thumbnailLink and webContentLink. Everything is working fine except that when I switch to v3 I don't get the response I should. The documentation says I should get a metadata response like https://developers.google.com/drive/v3/reference/files
but I only get: id, kind, name and mimeType. What am I doing wrong?
As stated in Migrate to Google Drive API v3 documentation, there are changes on how fields were returned.
Full resources are no longer returned by default. You need to use the fields query parameter to request specific fields to be returned. If left unspecified only a subset of commonly used fields are returned.
You can see examples on Github. This SO question might also help.
In v3 they made all the queries parametric. So you can query passing some parameter like
var request = gapi.client.drive.files.list({
'pageSize': 10,
'fields': 'files,kind,nextPageToken'
});
This block of code will return you all the information of every file just like v2.
If you are sending a get request then for fetching all the information you can try GET https://www.googleapis.com/drive/v3/files?fields=files%2Ckind%2CnextPageToken&key={YOUR_API_KEY}
Suppose you need ownsers and permissions only then set
var request = gapi.client.drive.files.list({
'pageSize': 10,
'fields':'files(owners,permissions),kind,nextPageToken'
});
For GET request use GET https://www.googleapis.com/drive/v3/files?fields=files(owners%2Cpermissions)%2Ckind%2CnextPageToken&key={YOUR_API_KEY}
for reference you can use Google Developers Documentation for fetching File list

Call URL with Basic Authentication with Accept and Content Type

I have the following code in my mobile application, and it fetches data properly. myURL is symbolic only.
var client = new RestClient("http://myURL/");
client.Authenticator = new HttpBasicAuthenticator("efgh", "ijkl");
var request2 = new RestRequest("rest/humantask/task/assigned/Jane");
request2.AddHeader ("Accept", "application/json");
request2.AddHeader ("Content-Type", "application/json");
However, I would like to see json objects on my browser. Once I call the URL as follows, it does not return anything. What am I missing? or is there a tool (extension) which I could use?
myURL/rest/humantask/task/assigned/Jane
hello for crome i preffer JSON Editor Extenstion you can also edit JSON Result. you can check JSONEditor
I have found a solution by using postman, there is username and password sections, and also header entries.
Postman Link could be found in the following link:
https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

HTTP Status 0 from AngularJS Get for JSON

I'm running a $http.get for a JSON and am getting a status of 0. I've downloaded the same JSON and the get works locally, and in Python using the requests library I can get the JSON no problem, but in AngularJS it's not working. What I don't understand is why angular isn't getting it but everything else is. Code snippet below.
function AgentListCtrl($scope, $http) {
$http.get('http://foo.bar/api/objects').success(function(data) {
$scope.objects = data;
}).error(function(data, status) {
$scope.status1 = status;
});
This provides the JSON and parses it when using a local file, but otherwise it fails and sets status1 to 0.
Just to make this clear since is not directly stated in the above answer (but in its comments) and, like me, some Angular newbies may be spending some time on this:
Angular's $resource will be able to execute a REST verb on another server, which in turn will respond correctly (with a status 200). Angular will nevertheless fail with a cryptical message, identifyiable by the status 0. It is further misleading since, in a browser's debugger, you may actually see the server's answer.
Angular will do an OPTIONS request on a cross-domain request (at least for the default query() method) unless specified on the contrary. Usually the server will not answer with the desired content (i.e. your representation). One simple way of doing this per request is specifying the method to be 'GET'.
$resource('http://yourserver/yourentity/:id', {}, {query: {method: 'GET'});
The server answering your REST requests MUST include the headers specified by CORS [1] in order to allow Angular to consume properly the response. Essentially this means including the Access-Control-Allow-Origin header in your response, specifying the servers from where the request comes from, that are allowed. This value may be *.
Complementing this answer for anyone integrating AngularJS with spring-data-rest-webmvc:
the HATEOAS json formatted response will not be properly consumed by Angular, producing instead the error Expected response to contain an array but got an object. This is solved by adding the isArray: false parameter to the $resouce's configuration;
a very to-the-point example of configuring CORS for the spring-data-rest-webmvc scenario is presented at [2] (see the SimpleCORSFilter)
[1] https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS
[2] https://spring.io/guides/gs/rest-service-cors/
In your code, the status assignment only occurs when the error happens. You should be able to get the status when the call was made successfully like this:
success(function(data, status, headers, config) {
$scope.objects = data;
$scope.status1 = status;
}).error(function(data, status) {
$scope.status1 = status;
});
I was having a similar problem myself. A third party API that returns JSON just fine through every other means was failing with status 0 when called through Angular's $http.get() method.
In my case there wasn't any CORS problem. Instead, the URL I was using for the API was not quite right and the server was issuing a 301 response. Angular wasn't respecting the redirect.
Word to the wise.

Google dictionary api does not return pure json?

I have the following code:
var json = new WebClient().DownloadString(string.Format(#"http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q={0}&sl=en&tl=en", "bar"));
This returns something like this:
dict_api.callbacks.id100({"query":"bar","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"bar",....
Why is it returning a function rather than just the json? Am I using web client incorrectly?
As I understand it, this is JSONP - JSON which is "padded" with a function call to allow cross-domain data transfer. I strongly suspect that if you pass in a different callback name on the URL, you'll see that other name come back in the response.
(Note that although I work for Google, this answer is not an "official" response from Google in any way, shape or form.)
You may want to use check this out :
json_decode for Google Dictionary API
They actually modify the resultant jsonp to get a json