Difference between XPOST and XPUT - rest

I'm just starting to use ElasticSearch. And I tried to know how to insert documents. I only found examples using the PUT method : $ curl -XPUT 'http://localhost:9200/...'
But it also seems to work using POST. Is there any difference between these two methods?
Thank you.

Generally when using a REST API:
- POST is used to create a resource, where the server will pick an ID.
- PUT is used to update OR PLACE a resource at a known ID.
Doc creation examples in the ES documentation show the caller picking an ID.
Like so:
curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
Since the caller is picking the ID a PUT seems appropriate here.
BUT
using POST Elasticsearch can also generate an ID for you.
$ curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'

Somehow they have the same functionality with small different
PUT verb (“store this document at this URL”)
POST verb (“store this document under this URL”)
in the put you must indicate the exact URL but in the post you can set document by just type
for example:
PUT /website/blog/123 says put this document at exact this URL but POST /website/blog will insert the document in blog and auto increment the id of the last document.

Related

Rest API Best Practice - Single action and Bulk

Let my server have the ability to perform an action called 'A'.
Now, My server needs to have an extra ability to perform bulk 'A' actions.
The route on the server is:
/entity/:entityId/'A'/:'A'Id
Adding the bulk ability faced me with two approaches:
1) Exposing 2 routes to each method:
/entity/:entityId/'A'/:'A'Id and
/entity/:entityId/'A' with a list of 'A' ids in the request's body.
2) Drop the 'A'Id paramater and add a query parameter to the first route called bulk with boolean value:
/entity/:entityId/'A'/?bulk=boolean
And if bulk == true look for 'A'Id[] at the request's body.
else if bulk == false look for id entry at the request's body.
I'm feeling that the 1st approach is better, I'd love to hear thoughts, or maybe a very different approach.
Any opinion is blessed to hear,
Thanks.
Query params are good for GET methods like:
curl -X GET host.com/megacorp/employee?employee_id[]=1&employee_id[]=2
But for POST and PUT methods it's better to use something like this:
curl -XPOST host.com/megacorp/employee/_bulk -d '{"data":[
{"id":"1", "name": "John Doe"},
{"id":"2", "name": "Jane Doe"}
]}'
And to POST or PUT 1 resource - simply provide 1 object in request, like:
curl -XPUT host.com/megacorp/employee/1 -d '{
"name": "JOHN DOE"
},'

Get ads and their image in the same query

I'm using the graph API and trying to get a list of ads with their insights and post images.
I don't want to do multiple queries for this as I quickly hit the "(#17) User request limit reached" issue even when I use batch queries.
My current query looks like this:
/ACCOUNT_ID_HERE/ads?fields=insights{cpc,reach,spend,clicks,ctr},status,creative
Now in order to get the post image, I need to take the creative ID that is returned and use it in another query to pull the post like this:
/CREATIVE_ID/?fields=object_story_id
Then use the returned story id to pull the picture like:
/OBJECT_STORY_ID/?fields=picture
Is there any way I can combine these queries to do less requests?
Something like:
/ACCOUNT_ID_HERE/ads?fields=insights{cpc,reach,spend,clicks,ctr},status,creative{object_story_id{picture}}'
Any help is appreciated. Thanks.
Facebook's Batch API may work for you. It allows for multiple Graph API calls to be made from a single HTTP request and supports dependencies between those requests. Read over the documentation for details and here's a example curl call of how it might work (I've not executed this call so please review against the API documentation and test).
curl \
-F 'access_token=...' \
-F 'batch=[
{
"method":"GET",
"name":"ads",
"relative_url":"/ACCOUNT_ID_HERE/ads?fields=insights{cpc,reach,spend,clicks,ctr},status,creative",
},
{
"method":"GET",
"name":"creative",
"relative_url":"/{result=ads:$.data.*.creative}/?fields=object_story_id"
},
{
"method":"GET",
"relative_url":"/{result=creative:$.data.*.object_story_id}/?fields=picture"
}
]' \
https://graph.facebook.com

Send advanced push notification in parse.com with Rest API

Im trying to send push with two criteria in where.
I make this so:
curl -X POST
-H "X-Parse-Application-Id: myappId" \
-H "X-Parse-REST-API-Key: myRESTApiId" \
-H "Content-Type: application/json" \
-d '{
"where": {“$and”:[{“deviceType": "winphone”},{”channels":{"$all":[“string1”],"$nin":[“string2”]}}]},
"data": {"alert": “String1 is comming”}
}' \
https://api.parse.com/1/push
Something like: https://parse.com/questions/rest-api-or-constraint-on-multiple-fields-using-where, but I getting error message: error code 107 - invalid JSON Parse
How can I send push notification for given device and for given channel with condition $all and $nin.
Thanks for your help!
Hipek
This error is likely being returned because your where value does not match the REST API spec. You will also want to make sure you are consistent in your use of double quotes as these can also lead to malformed JSON errors (e.g. do not use “ and ”, use ").
After fixing that, we end up with the following, which is still not valid per the REST API Parse docs:
"where": {
"$and": [
{"deviceType": "winphone”},
{"channels": {
"$all": ["string1"],
"$nin":["string2"]}
}
]
},
There's a couple of problems with your query:
$and is not a valid Parse REST API operator, and does not appear in the REST API docs. All constraints in a where query are implicitly ANDed, so this is unnecessary, anyway.
Your $all and $nin constraints over channels conflict with each other as there cannot be more than one such query per key. You may want to instead create a unique channel for those installations that should receive messages aimed at the string1 channel but not the string2 channel.

Localization of app-owned Open Graph objects

The localization procedure for Open Graph is tightly coupled to HTTP. There is a magical facebook HTTP-header or URL parameter given when scraping which should be used to change the value of localized data.
When using app-owned objects, the object is simply created using JSON. The question I have is: how do I localize an app-owned object?
Here is an example of the creation of an app-owned object. What can I do to localize the title?
$ curl -X POST "https://graph.facebook.com/app/objects/mynamespace:myobject" \
-F "$ACCESS" \
-F 'object = { "title" : "My Unlocalized Title",
"image" : "https://example.com/myimage.png",
"url" : "https://example.com/myobject" }'
In Android an id is returned in the Callback. Then I think you can use this id to localize the title. Sorry if I dont answer your question.
https://developers.facebook.com/docs/opengraph/guides/internationalization/#hosted

sinatra + mongoid

I'm creating an app that use sinatra + mongoid. I have two models, contact has many phones. To test my sinatra controller I post my data with this command `
curl -X POST -d "contact[name]=nome&contact[email]=email#dominio.com&contact[phone][0][number]=88888888&contact[phone][0][type]=1&contact[phone][2][number]=77777777&contact[phone][3][type]=1"
but when I did one query in mongodb I see that not save as expected. I need that phone class will be save with array, but now phone is a hash where the key is "0", "1", N like my post data. How do I to resolve this problem? I want data to be saved so:
{
"_id":"4f889875b336e722a0000003",
"email":"diego.dias2#dominio.com.br",
"github":"diegodfsd",
"name":"diego2",
"phone":{
"0":{
"number":"89311768",
"type":"cellphone"
},
"1":{
"number":"55555555",
"type":"home"
}
},
"twitter":"diegodfsd"
}
gist
You need use phones_attributes params instead of phone
curl -X POST -d "contact[name]=nome&contact[email]=email#dominio.com&contact[phones_attributes][0][number]=88888888&contact[phones_attributes][0][type]=1&contact[phones_attributes][2][number]=77777777&contact[phones_attributes][3][type]=1"