1

I have the following CURL syntax which I am trying to use by mimicking it in python requests:

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

I try it now using python requests:

import requests
import json
url = 'http://localhost:8983/solr/my_collection/update/json/docs'
payload = {"id": "1","title": "Doc 1"}
headers = {'Content-type': 'application/json'}

r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.text

the request.post fails. I am wondering if I need to somehow include the --data-binary flag in requests, but I am unsure.

Any help or pointers to the solution would be great.

Thanks.

EDIT

for r.text, I get:

{"responseHeader":{"status":0,"QTime":5}}
AJW
  • 5,569
  • 10
  • 44
  • 57
  • have you tried `requests.post(url, json=payload, headers=headers)` use **json** instead of data – Macintosh_89 Sep 30 '16 at 13:07
  • @Macintosh_89: Tried it. I get Status:400, cannot parse json. – AJW Sep 30 '16 at 13:11
  • I don't think you need a header to state the content type. can you try without headers? – Macintosh_89 Sep 30 '16 at 13:12
  • @Macintosh_89: tried it now - I get the same i.e status:0 – AJW Sep 30 '16 at 13:14
  • @e4c5: I have added the resultant response code. Does this help? – AJW Sep 30 '16 at 13:14
  • Possible duplicate of [Conversion of curl to python Requests](http://stackoverflow.com/questions/20461194/conversion-of-curl-to-python-requests) – vabada Sep 30 '16 at 13:15
  • Certainly helps, it would be a lot better if you could include the http status code and the response that you got from curl. You have not tagged this as solr so at the moment people who are reading it are not neccasarily people with solr knowlege but your present from of the question requires it to answer – e4c5 Sep 30 '16 at 13:17
  • @e4c5: I have found out what the problem is. Your comment certainly helped. So, originally, I took the CURL command off the solr docs, BUT, i did not realize that commit was not set to true in the url - I blame the damn docs. You can look here: https://cwiki.apache.org/confluence/display/solr/Uploading+Data+with+Index+Handlers I added this and it all works as expected. I was really stupid. Please post this as an answer and I will accept it. – AJW Sep 30 '16 at 14:08
  • Thanks that's very kind of you, But I am not exactly sure what to post as an answer. Please be so kind as to post your own answer and accept it. But feel free to upvote one of my answers to another question :-) – e4c5 Sep 30 '16 at 14:09

0 Answers0