Why is wordpress API sending back unpredictable results? - wordpress-rest-api

When using wordpress API, I'm sending the following request:
/posts?_embed=wp:featuredmedia&_fields=link,title,featured_media,date,description,_links,_embedded&categories[]=415&per_page=1
In reality, there are no posts under category 415, so I would expect the result to be an empty array. The problem is that I end up with an actual result which is a single post that belongs to another category.
I also noticed that changing things in the query params (I've tried removing one of the _fields params, removing the _embed I'm passing leads to different results being fetched. Any ideas if this has to do with the way I'm using the endpoint?

Related

Determine the proper REST API method

I have the following functionalities in my API:
Getting a user by their name
Getting a user by their ID
Getting a user, or if it doesn't exist create one
Getting multiple users by their ID
Currently I'm handling the two first functionalities with a GET request, and the third with a POST request. I could use a GET request for getting multiple users, but sending potentially hundreds of IDs through a query parameter seems like the wrong approach, as I would get a very long URL. I could also use a POST request to send the long list of IDs through its body, but I doubt a POST request is meant for this purpose.
What method would be appropriate to use for the last one?
I see 2 possible ways here:
Get all users and do your filtering post response.
Get a range of IDs, which resumes to only 2 parameters, the lower and the upper limits of the interval. (If this satisfy your needs)
Behaving the way you described and avoiding long URLs in the same time will not work together.

Passing sizable data in an REST GET request

A REST question. Let's say I have a database will a million items in it. I want to retrieve say 10,000 of them via an REST GET, passing in the GET request the ID's of the 10,000 items. Using URL request query parameters, it'll quickly exceed the maximum length of a URL. How does people solve this? Use a POST instead and pass it in the body? That seems hacky.
You should not address this form through the URL parameters, it has a limit: 2000 characters
Url limit
I guess what you are doing is something like this:
https://localhost/api/applicationcollections/(47495dde-67d2-4854-add0-7e25b2fe88e4,1b470799-cc8a-4284-9ca7-76dc34a5aebb)
If you are planning to get more than 10k records you can pass the information on the body of the request which doesn't have any limit. Technically speaking you should do it through a POST request, but that is not the intent with the semantic of the POST verb. Even for the GET you can include a body:HTTP GET with request body but it should not consider as part of the semantic.
Normally you don't filter 10k elements by id, instead, you get 10k elements on a request, passing a pagination parameter if you want through the URL, but that can kill your app, especially considering that the DTO has more than one field, like
ApplicationDto
field1
field2....
field15
Bellow, you have an example of how to pass pagination parameters and get the first 10k records
https://localhost:44390/api/applications?pageNumber=1&pageSize=10000
Also, the APIs should return an extra header, let's call it X-Pagination where you can get the information if you have more pages to paginate, including as well the total amount of elements.
As an extra effort to reduce the size of the request, you can shape the data and only get the fields you need.
ApplicationDto should bring only: field1, field3 see bellow:
https://localhost:44390/api/applications?fields=field1,field3
See how Twitter address this problem as well:
Twitter cursor response
Hope this helps

Retrieving batch FB reactions from multiple posts

I managed to return the first page of my posts with some details including its ID with:
api = 'https://graph.facebook.com/v3.2/me'
key = 'secret'
payload = {'fields':'''
posts{permalink_url,created_time,admin_creator,message}'''
, 'access_token':key}
r = requests.get(api, params = payload)
In the FB Graph API documentation, you can do an API call like this /{object-id}/reactions, so now with the ID, getting the reactions is possible. But I need to get the reaction counts by type, so after some research, I find out that I will be able to chain the calls like below.
reactions.type(LIKE).limit(0).summary(1).as(LIKE),
reactions.type(LOVE).limit(0).summary(1).as(LOVE),
reactions.type(WOW).limit(0).summary(1).as(WOW),
reactions.type(HAHA).limit(0).summary(1).as(HAHA),
reactions.type(SAD).limit(0).summary(1).as(SAD),
reactions.type(ANGRY).limit(0).summary(1).as(ANGRY),
reactions.type(THANKFUL).limit(0).summary(1).as(THANKFUL)
Fine now everything is working, but after a pause, I realized that no way in FB will allow me to run those calls for each post especially after I get all the posts IDs since the beginning and sure enough it does not.
So there's this thing about batching as well where I tried to do, where I stacked multiple IDs;
fields=id,reactions.type(PRIDE).limit(0).summary(1),reactions.type(LIKE).limit(0).summary(1)&ids=id1,id2
but I get this instead;
"message": "Syntax error \"Field reactions specified more than once. This is only possible before version 2.1\"
From here it is quite clear to me that FB does not allow specifying a field more than once but just doing reactions.limit(0).summary(1) just returns the total reactions, how am I able to retrieve the reactions from each post without having to bust the API. It gets more confusing as the APIs constantly change throughout time and now we have v3.2 and most of the methods I have researched on SO does not apply anymore.

Rest POST VS GET if payload is huge

I understand the definition of GET and POST as below.
GET: List the members of the collection, complete with their member URIs for further navigation. For example, list all the cars for sale.
POST: Create a new entry in the collection where the ID is assigned automatically by the collection. The ID created is usually included as part of the data returned by this operation.
MY API searches for some detail in server with huge request payload with JSON Message in that case Which Verb should i use ?
Also can anyone please let me know the length of the characters that can be passed in query string.
The main difference between a GET and POST request is that in the former, the entire request is encoded as part of the URL itself, whereas in the latter, parameters are sent after the header. In addition, in GET request, different browsers will impose different limits on how big the URL can be. Most modern browsers will allow at least 200KB, however Internet Explorer seems to limit the URL size to 2KB.
That being said, if you have any suspicion that you will be passing in a large number of parameters which could exceed the limit imposed on GET requests by the receiving web server, you should switch to POST instead.
Here is a site which surveyed the GET behavior of most modern browsers, and it is worth a read.
Late to the party but for anyone searching for a solution, this might help.
I just came up with 2 different strategies to solve this problem. I'll create proof of concept API and test which one suites me better. Here are the solution I'm currently thinking:
1. X-HTTP-Method-Override:
Basically we would tunnel a GET request using POST/PUT method, with added X-HTTP-Method-Override request header, so that server routes the request to GET call. Simple to implement and does work in one trip.
2. Divide and Rule:
Divide requests into two separate requests. Send a POST/PUT request with all payload, to which server will create necessary response and store it in cache/db along with a key/id to access the data. Then server will respond with either "Location" header or the Key/id through which the stored response can be accessed.
Now send GET request with the key/location given by server on previous POST request. A bit complicated to implement and needs two requests, also requires a separate strategy to clean the cached responses.
If this is going to be a typical situation for your API then a RESTful approach could be to POST query data to a buffer endpoint which returns a URI from which you can GET your results.
Who knows maybe a cache of these will mitigate the need to send "huge" blobs of data about.
Well You Can Use Both To get Results From Server By Passing Some Data To server
In Case Of One Or Two Parameters like Id
Here Only One Parameter Is Used .But 3 to 4 params can Be used This Is How I Used In angularjs
Prefer : Get
Example : $http.get('/getEmployeeDataById?id=22');
In Case It Is Big Json Object
Prefer : Post
Example : var dataObj =
{
name : $scope.name,
age : $scope.age,
headoffice : $scope.headoffice
};
var res = $http.post('/getEmployeesList', dataObj);
And For Size Of Characters That Can Be Passed In Query String Here Is Already Answered
If you're getting data from the server, use GET. If you want to post something, use POST. Payload size is irrelevent. If you want to work with smaller payloads, you could implement pagination.

How do i get the list of all the leads in Marketo

I want to get all leads in Marketo using their Rest Apis. Is there a way to do this? I have already tried the getLeadChanges api, however that only returns leads with changed fields.
Leads in Marketo are assigned lead ids in sequential order starting with 1. Using the Get Multiple Leads by Filter Type REST API endpoint, you can query 300 leads by lead id with each call.
You will have to specify id as the filterType and the lead ids as the filterValues with each call to this endpoint. To get all leads, you would iterate through the total number of leads 300 at a time.
The first API call would be (replace ... with all the values in between):
/rest/v1/leads.json?filterType=Id&filterValues=1,2,3,...,298,299,300
The second API call, and each subsequent API call would follow the same pattern:
/rest/v1/leads.json?filterType=Id&filterValues=301,302,303,...,598,599,600
By Marketo REST API you can request only static lists, and the "All leads" is dynamic.
Easyest way will be:
Create static list
Add all existing leads to this list
Create a Marketo campaign to add every new lead to this Static list.
then just query leads in this list. e.g.: https://%yourSubdomain%.mktorest.com/rest/v1/lists/%listId%/leads.json
Hope it will help.