Suliman
Suliman

Reputation: 1509

How to insert data to ArangoDB with HTTP?

Could you help me to understand how to insert data in existing collection. Collection name is cars. I am trying to insert new data to it. I am doing like:

curl -X PUT --data-binary @- --dump - http://localhost:8529/_db/testdb/_api/collection/cars/ '{name: "carname"}'

But after it nothing happens. Look like I miss syntax.

Am I right understand that I should use /_api/cursor only when I am doing get request to DB like

{"query": "FOR car IN cars RETURN car"}

Upvotes: 1

Views: 904

Answers (2)

stj
stj

Reputation: 9107

With the @- in the command curl --data-binary @- ... you're asking curl to read from stdin. That means the command will not do anything and not complete until you press CTRL-D. However, if you press CTRL-D, it will send the data to the server.

It is possible to specify the data to send without reading it from stdin. That can be done via curl --data-binary <data>, with <data> being the JSON to send. Note that it needs to be valid JSON and correctly escaped in the shell you use. '{name: "carname"}' from the above example is no correct JSON (the quotes around attribute name name are missing). It should rather be '{"name": "carname"}'.

As @yojimbo87 pointed out, insert operations should be sent via HTTP POST rather than PUT, and the collection name should be passed as a URL parameter.

Here is the full command:

curl -X POST --data-binary '{"name": "carname"}' --dump - "http://localhost:8529/_db/testdb/_api/document?collection=cars"

The above will work in Bash, however, it won't work from the Windows command-line because strings there shouldn't be enclosed in ', but rather ".

Upvotes: 2

yojimbo87
yojimbo87

Reputation: 68443

If you want to insert new data into existing collection then you have to use POST verb, for example like this:

curl -X POST -d "{ \"field\": \"value\" }" --dump - http://localhost:8529/_db/myDatabase/_api/document?collection=cars

PUT verb is used for replacement of existing documents. More examples can be found in docs under Working with Documents using REST.

Cursors are used for executing AQL queries through REST API and they use POST, PUT and DELETE verbs.

Upvotes: 3

Related Questions