如何从python发出curl post请求

时间:2016-06-14 10:18:12

标签: python rest post curl

我正在尝试使用python中的curl发布帖子请求,但下面的脚本会抛出错误

import os

first_name1 = "raj"
last_name1 = "kiran"
full_name = "raj kiran"
headline = "astd"
location1 = "USA"
current_company1 = "ss"

curl_req = 'curl -X POST -H "Content-Type: application/json" -d '{"first_name":"{0}","last_name":"{1}","current_company":"{2}","title":"{3}","campus":"","location":"{4}","display_name":"{5}","find_personal_email":"true"}' http://localhost:8090'.format(first_name1,last_name1,current_company1,headline,location1,full_name)

os.popen(curl_req)

错误:

SyntaxError: invalid syntax

如何使上述程序有效?

2 个答案:

答案 0 :(得分:1)

代码中的问题是引号。将其更改为:

SortedComboBoxModel

但是,正如评论中所提到的,String[] items = { "one", "two", "three" }; SortedComboBoxModel<String> model = new SortedComboBoxModel<String>(items); JComboBox<String> comboBox = new JComboBox<String>(model); comboBox.addItem("four"); comboBox.addItem("five"); comboBox.setSelectedItem("one"); 永远是更好的选择。

请求语法:

curl_req = '''curl -X POST -H "Content-Type: application/json" -d '{"first_name":"{0}","last_name":"{1}","current_company":"{2}","title":"{3}","campus":"","location":"{4}","display_name":"{5}","find_personal_email":"true"}' http://localhost:8090'''.format(first_name1,last_name1,current_company1,headline,location1,full_name)

答案 1 :(得分:1)

我用于将cURL请求转换为Python请求的一个好资源是curlconverter。您可以输入您的cURL请求,并将其格式化为Python requests

作为旁注,它还可以转换为PHP和Node.js。

希望有所帮助!