Stack Exchange API - getting answers for the questions object - rest

I use https://api.stackexchange.com/docs. To get a sample question, I use the address https://api.stackexchange.com/2.2/questions/6827752?&site=stackoverflow. Thanks to this, he gains a question. The following page shows https://api.stackexchange.com/docs/types/question that with the default filter the question will not have a body. I know I use a filter to get a body filter=withbody that is, the address comes out
https://api.stackexchange.com/2.2/questions/6827752?&site=stackoverflow&filter=withbody
Now I would like to get answers for the question. That's why I want to use a filter filter=withanswers. This address
https://api.stackexchange.com/2.2/questions/6827752?&site=stackoverflow&filter=withanswers
returns an error
{
"error_id": 400,
"error_message": "Invalid filter specified",
"error_name": "bad_parameter"
}
How do get answers or comments using a filter?

I think that 2 patterns can be thought for your solution. One is a method using custom filter. Another is a method using the endpoint for retrieving answers.
Pattern 1 :
You can create a custom filter for retrieving answers and answer's body using the endpoint of https://api.stackexchange.com/2.2/questions/### questionId ###.
You can create the custom filters at http://api.stackexchange.com/docs/create-filter.
In your case, you can use this.
question.answers;answer.body;question.body was used as including filters.
The delimiter is ;.
As a result, you retrieve "filter": "!T*hPNRA69ofM1izkPP".
When you use the filter, please do URL encode it.
!T*hPNRA69ofM1izkPP becomes %21T%2ahPNRA69ofM1izkPP.
You can retrieve answers and answer's body using the endpoint.
When the question ID of your question is used, it's https://api.stackexchange.com/2.2/questions/6827752?&site=stackoverflow&filter=%21T%2ahPNRA69ofM1izkPP.
Result of pattern 1
When you can access to https://api.stackexchange.com/2.2/questions/6827752?&site=stackoverflow&filter=%21T%2ahPNRA69ofM1izkPP, you can get answers and answer's body.
Pattern 2 :
As the endpoint for retrieving answers, you can use /questions/{ids}/answers. If the question ID is 6827752 in your question, the endpoint is as follows. In this case, in order to retrieve answer's body, filter=withbody is used.
Result of pattern 2
When you can access to https://api.stackexchange.com/2.2/questions/6827752/answers?order=desc&sort=activity&site=stackoverflow&filter=withbody, you can get answer's body.
If this was not useful for you, I'm sorry.

Related

Unable to retrieve 'ContentId' property of Attachment in Office365 REST Api

I'm trying to retrieve attachments in the Office365 rest api. Since I want to avoid downloading the entire attachments, I'm using a select clause to avoid downloading the content, which is in the ContentBytes property:
$select="ContentId,ContentType,Id,IsInline,Name,Size"
So basically, I want to retrieve everything except the content. However, this gives the following error message (json):
{
"error":
{
"code": "RequestBroker-ParseUri",
"message": "Could not find a property named 'ContentId' on type 'Microsoft.OutlookServices.Attachment'."
}
}
It's telling me that ContentId doesn't exist, which contradicts the specifications.
Edit: Here is the full request:
GET /api/v2.0/me/messages/AAMkAGZlZjI3N2I3LTg1YWUtNDFiNC05MGI0LTVjYTVmZGI5NGI2YQBGAAAAAABzr8uDji9LRqgTCEsDv22wBwBWTXbvZW0dTKuxUGxpK4-lAAAAAAEMAABWTXbvZW0dTKuxUGxpK4-lAAC5QnKBAAA=/attachments?%24select=ContentId%2CContentType%2CId%2CIsInline%2CName%2CSize
Even more strange, when I do the same query without specifying any select clause, it returns me a full attachment object, including a ContentId.
Anybody can help?
In case anyone has the same question for microsoft graph, you need to pass this filter:
$select=microsoft.graph.fileAttachment/contentId
like this:
GET https://graph.microsoft.com/v1.0/me/messages/attachments?$select=microsoft.graph.fileAttachment/contentId
The request that you posted is getting the message specifications but not the attachments. Since you need to get the content id, you need to add /attachments to the request with any required parameters.
GET https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments/{attachment_id}
So please add the attachments to your query to be able to get the content id.
Hope this helps.
Solved it. The answer was suggested by Brian's comment and I found an additional hint here.
Since 'ContentId' is a property of a FileAttachment, you need to specify that in the request, like so:
$select="Microsoft.OutlookServices.FileAttachment/ContentId,ContentType,Id,IsInline,Name,Size"
That did the trick. Thanks for the suggestions.

Why does one HTTP GET request retrieve the required data and another retrieve []

I'm currently working on ng-admin.
I'm having a problem retrieving user data from my REST API (connected to a MongoDB) and displaying it.
I have identified the problem as the following:
When I enter http://localhost:3000/users into my browser, I get a list of all users in my database.
When I enter http://localhost:3000/users?_page=1&_perPage=30&_sortDir=DESC&_sortField=id,
I get [] as a result.
I am quite new to this, I used both my browser and the POSTMAN Chrome extension to test this and get the same result.
http://localhost:3000/users_end=30&_order=DESC&_sort=id&_start=0
This (/users_end) is a different request than /users.
It should be:
http://localhost:3000/users?end=30&_order=DESC&_sort=id&_start=0
Or, by looking at the other parameters:
http://localhost:3000/users?_end=30&_order=DESC&_sort=id&_start=0
with end or _end being the first parameter (mark the ?).
Update (it is ? and before the _, I have edited.):
If adding parameters to the request returns an empty list, try adding only one at a time to narrow down the problem (there's probably an error in the usage of those parameters - are you sure you need those underscores?).
Your REST API must have a way to handle pagination, sorting, and filtering. But ng-admin cannot determine exactly how, because REST is a style and not a standard. So ng-admin makes assumptions about how your API does that by default, that's why it adds these _end and _sort query parameters.
In order to transform these parameters into those that your API understands, you'll have to add an interceptor. This is all thoroughly explained in the ng-admin documentation: http://ng-admin-book.marmelab.com/doc/API-mapping.html

How can I get multiple %-encoded parameters into an apiary URI template?

The ApiaryIO spec—actually the RFC to which it points—indicates that you cannot use "." in a parameter name, you need to encode it to "%2E". That's fine, but there seems to be a bug where Apiary can only handle one such encoding. For example, the following
## Notes Collection [/notes{?foo%2Ebar}]
yields the following Code Example
request = Request('http://private-d1ee7-testingnewapiary.apiary-mock.com/notes?foo.bar=foo.bar')
which is correct. However, the following
## Notes Collection [/notes{?foo%2Ebar,baz%2Ebla}]
yields this Code Example:
request = Request('http://private-d1ee7-testingnewapiary.apiary-mock.com/notes?foo%252Ebar=foo%252Ebar&baz%252Ebla=baz%252Ebla')
Notice how in the first the Code Example you see it has "foo.bar" but in the second example it has "foo%252Ebar", which is incorrect.
The downstream effect here is that the incorrect URI is sent to the API server so the response is malformatted creating an error.
How do I encode many "."-containing parameters on the URI template and still get the proper code examples?
Will adding explicit example values for those parameters help?
For example:
## Notes Collection [/notes{?foo%2Ebar,baz%2Ebla}]
+ Parameters
+ foo%2Ebar (`42`)
+ baz%2Ebla (`24`)
Update
This seems to be a bug in the way the documentation / code samples are rendered. I have created the tracking issue here https://github.com/apiaryio/language-templates/issues/36.

How do I use a CouchDB username in a URL rewrite?

I have a list function that can be accessed like this:
_list/characters/characters_by_user?startkey=["org.couchdb.user:rodriguez"]&endkey=["org.couchdb.user:rodriguez", {}]
I'm attempting to rewrite the URL so you can access it in a friendlier way...
/rodriguez/characters
...by writing this rule:
{from: '/:user/characters', to: '_list/characters/characters_by_user',
query: {
startkey: "[%22org.couchdb.user%3A:user%22]",
endkey: "[%22org.couchdb.user%3A:user%22,%20{}]"
}
}
However, I get this error:
error: "query_parse_error",
reason: "No rows can match your key range, reverse your start_key and end_key or set descending=true"
Why would the query work correctly in the full URL, but not using the rewrite?
Update
I believe this may be a bug with the way CouchDB handles the encoded colon (%3A). I'm awaiting a response from the mailing list and will update this question accordingly.
I found that checking CouchDB's logs proved to be the best way to troubleshoot how URLs were being rewritten. Unfortunately, the issue I submitted to the CouchDB mailing list has yet to be replied to.
As a workaround, I've emitted the user's name without fully-qualifying it, which suits my purpose:
var user = doc.createdBy.split(":")[1];
emit(user, doc);

How to get exact phrase match out of Graph API search

Looking for a way to get an exact phrase match out of the Graph API's search endpoint. For example, all activities with "dogs and cats" in them. Putting the phrase in quotes doesn't seem to work, the API will return activities containing those words, but in any order.
curl -v "https://graph.facebook.com/search?q=%22dogs%20and%20cats%22&type=post&limit=75&access_token=&since=Wed+Jan+25+20%3A59%3A30&until=Wed+Jan+25+20%3A59%3A40"
returns and activity whose text is:
"Ohhh man it's raining dogs cats lobsters crab birds and horses up here. I'm scared!"
"dogs" "cats" and "and" are all in that post, but not in order.
Yeah I faced the same problem. There's a similar question that might help Using the Facebook Graph API to search for an exact string
Facebook doesnt allow exact phrasal matching drectly, atleast not at the API level, You would have to fetch the entire data and programmically check for exact matches (too slow though).