3

How do I can send JSON object to solr collection usinc cUrl

I'm using Windows 10.

curl -X POST -H "Content-Type:application/json" "http://localhost:8983/solr/solr_sample/update/json/docs" --data-binary "{'id': '1','title':'Doc 1'}"

When I'm using this format I'm getting some kind of warning message:

curl -X POST -H 'Content-Type:application/json' 'http://localhost:8983/solr/sorl_sample/update/json/docs' --data-binary '{"id": "1","title":"Doc 1"}'
curl: (1) Protocol "'http" not supported or disabled in libcurl
curl: (3) [globbing] unmatched close brace/bracket in column 14

I resolved it using " " insted of ' '

When I send the request using the first url I'm getting this response:

{
  "responseHeader":{
    "status":0,
    "QTime":112}}

But when I try to get some result by searching I can't see any object in docs[]

curl -X GET "http://localhost:8983/solr/solr_sample/select?q=*:*"

Result:

{
      "responseHeader":{
        "status":0,
        "QTime":0,
        "params":{
          "q":"*:*"}},
      "response":{"numFound":0,"start":0,"docs":[]
      }}

When I'm using Solr UI I can add JSON objects without any problems, also to see the result in terminal

2
  • You have to commit the update. Add commit=true to the request. Commented Mar 19, 2018 at 6:20
  • Do you mean like this? curl -X POST -H "Content-Type:application/json" "http://localhost:8983/solr/solr_sample/update?commit=true" --data- binary "{'id':'1'}" I'm getting this error: <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Error 404 Not Found</title> </head> <body><h2>HTTP ERROR 404</h2> <p>Problem accessing /solr/solr_sample/update. Reason: <pre> Not Found</pre></p> </body> </html> Commented Mar 19, 2018 at 8:27

1 Answer 1

9

You have to commit the update. Add commit=true to the URL path or "commitWithin":1000 to the JSON request, e. g.

curl -X POST \
    -d '{"add":{"doc":{"id":"delete.me","title":"change.me"},"commitWithin":1000}}' \
    -H "Content-Type: application/json" \
    http://localhost:8983/solr/solr_sample/update

Or using the URL:

curl -X POST \
    -d '{"add":{ "doc":{"id":"delete.me","title":"change.me"}}}' \
    -H "Content-Type: application/json" \
    http://localhost:8983/solr/solr_sample/update?commit=true
Sign up to request clarification or add additional context in comments.

4 Comments

Tester both commands and unfortunatelly doesn't work. I'm getting the error messages curl -X POST -d '{"add":{ "doc":{"id":"2","title":"Random title"}}}' -H "Content-Type:application/json" http://localhost:8983/solr/solr_sample/update?commit=true curl: (3) [globbing] unmatched close brace/bracket in column 29 Error 404 Not Found HTTP ERROR 404 Problem accessing /solr/solr_sample/update. Reason: Not Found
curl -X POST -d '{"add":{"doc":{"id":"2","title":"Title 2"},"commitWithin":1000}}' -H "Content-Type:application/json" http://localhost:8983/solr/solr_sample/update/ <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Error 404 Not Found</title> </head> <body><h2>HTTP ERROR 404</h2> <p>Problem accessing /solr/solr_sample/update/. Reason: <pre> Not Found</pre></p> </body> </html>
Try replacing the single quotes with double quotes, e. g. "{\"add\":{...}}" or "{'add':{...}}". Maybe curl does behave different on Windows than on UNIX and OS X, the platforms I am working on.
I just tested on Ubuntu 16 and works good. Thank you man, you are amazing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.