-1

I can't unserstand what I am doing wrong, but when I am sending next request with curl, I am getting error:

echo {"id":1,"question":"aaa"},{"id":2,"question":"bbb?"} | curl -X POST --data-binary @- --dump - http://localhost:8529/_db/otest/_api/document/?collection=sitetestanswers


HTTP/1.1 100 (Continue)

HTTP/1.1 400 Bad Request
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 100

{"error":true,"errorMessage":"failed to parse json object: expecting EOF","code":400,"errorNum":600}

Any ideas? I tied wrap it's to [...]. Nothing do not help. With [...] validator mark this as valid

Same with D. Here is my code:

void sendQuestionsToArangoDB(Json questions)
{
    string collectionUrl = "http://localhost:8529/_db/otest/_api/document/?collection=sitetestanswers"; 

    auto rq = Request();
    rq.verbosity = 2;
    string s = `{"id":"1","question":"foo?"},{"id":2}`;
    auto rs = rq.post(collectionUrl, s, "application/json");
    writeln("SENDED");
}

--

 POST /_db/otest/_api/document/?collection=sitetestanswers HTTP/1.1
 Content-Length: 37
 Connection: Close
 Host: localhost:8529
 Content-Type: application/json

 HTTP/1.1 400 Bad Request
 Server: ArangoDB
 Connection: Close
 Content-Type: application/json; charset=utf-8
 Content-Length: 100
 100 bytes of body received

For D I use this lib: https://github.com/ikod/dlang-requests Same issue with vibed.

Max Alibaev
  • 681
  • 7
  • 17
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145

3 Answers3

3

ArangoDB do not understand JSON if it's come ass array like [...]. It should be passed as key-value. So if you need pass array it should have key mykey : [].

Here is working code:

import std.stdio;
import requests.http;

void main(string[] args) 
{
    string collectionUrl = "http://localhost:8529/_db/otest/_api/document?collection=sitetestanswers"; 

    auto rq = Request();
    rq.verbosity = 2;
    string s = `{"some_data":[{"id":1, "question":"aaa"},{"id":2, "question":"bbb"}]}`;
    auto rs = rq.post(collectionUrl, s, "application/json");
    writeln("SENDED");
}

otest - DB name

sitetestanswers - collection name (should be created in DB)

Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
1
echo '[{"id":1,"question":"aaa"},{"id":2,"question":"bbb?"}]'

should do the trick. You need to put ticks around the JSON. The array brackets are necessary otherwise this is not valid JSON.

techraf
  • 64,883
  • 27
  • 193
  • 198
Kaveh Vahedipour
  • 3,412
  • 1
  • 14
  • 22
  • Like this: `echo '[{"id":1,"question":"aaa"},{"id":2,"question":"bbb?"}]' | curl -X POST --data-binary @- --dump - http://localhost:8529/_db/otest/_api/document/?collection=sitetestanswers` ? Same result :( – Dmitry Bubnenkov May 04 '16 at 20:23
  • 1
    this is what i get back: [{"_id":"sitetestanswers/939","_key":"939","_rev":"939"},{"_id":"sitetestanswers/943","_key":"943","_rev":"943"}]. i cut and pasted your post here. – Kaveh Vahedipour May 04 '16 at 20:27
  • what can be wrong? Any ideas? http://img.ctrlv.in/img/16/05/04/572a5d4335db6.png – Dmitry Bubnenkov May 04 '16 at 20:36
  • 1
    i was going to ask what shell you were using. i should have. i am not sure how to do proper windows shell escaping. you have the choice to use -d @data.json. put your json into the file without the ticks. – Kaveh Vahedipour May 04 '16 at 21:08
  • I do not think that it's problem in shell. I use ConEmu. The command with single json oblect is past fine: `echo {"id":2,"question":"bbb?"} | curl -X POST --data-binary @- --dump - http://localhost:8529/_db/otest/_api/document/?collection=sitetestanswers` the issue only when it's have two objects like example above – Dmitry Bubnenkov May 04 '16 at 21:15
  • 1
    it's sure the proper escaping. look here. http://stackoverflow.com/questions/28648473/windows-curl-with-json-data-on-the-command-line – Kaveh Vahedipour May 04 '16 at 21:20
1

You are trying to send multiple documents. The data in the original question separates the documents by comma ({"id":1,"question":"aaa"},{"id":2,"question":"bbb?"}) which is invalid JSON. Thus the failed to parse json object answer from ArangoDB.

Putting the documents into angular brackets ([ ... ]) as some of the commentors suggested will make the request payload valid JSON again.

However, you're sending the data to a server endpoint that handles a single document. The API for POST /_api/document/?collection=... currently accepts a single document at a time. It does not work with multiple documents in a single request. It expects a JSON object, and whenever it is sent something different it will respond with an error code.

If you're looking for batch inserts, please try the API POST /_api/import, described in the manual here: https://docs.arangodb.com/HttpBulkImports/ImportingSelfContained.html

This will work with multiple documents in a single request. ArangoDB 3.0 will also allow sending multiple documents to the POST /_api/document?collection=... API, but this version is not yet released. A technical preview will be available soon however.

stj
  • 9,037
  • 19
  • 33