У меня есть запрос curl, который правильно работает в командной строке в python3. API написан на flask остальные рамки.
curl -d '{"text":"Some text here.", "category":"some text here"}' -H "Content-Type: application/json" -X POST http://xx.xx.x.xx/endpoint
Я преобразовал этот запрос в запрос подпроцесса
txt = 'Some text here'
tmp_dict = {"text":txt, "category":"text"}
proc = subprocess.Popen(["curl", "-d", str(tmp_dict), '-H', "Content-Type: application/json", "-X", "POST", "http://xx.xx.x.xx/endpoint"], stdout=subprocess.PIPE)
(out, err) = proc.communicate()
out = eval(out.decode("utf-8"))
Ошибка (Я возвращаю ответ status_code
400, когда в запросе не передан текст, который явно не случай здесь)
Traceback (most recent call last):
File "/home/joel/.virtualenvs/p3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-35-324f7c5741fc>", line 6, in <module>
out = eval(out.decode("utf-8"))
File "<string>", line 1
NO TEXT INPUT
^
SyntaxError: invalid syntax
Когда я отправляю запрос с текстом, он работает правильно, но это не то, что я намерен сделать.
proc = subprocess.Popen(["curl", "-d", '{"text":"Some text here.", "title":"some text here"}', '-H', "Content-Type: application/json", "-X", "POST", "http://xx.xx.x.xx/endpoint"], stdout=subprocess.PIPE)