Python请求发布

Python请求发布,python,python-requests,Python,Python Requests,我正在使用一个没有文档的API,我遇到了一个绊脚石。我有一个功能: def add_to_publicaster(self): # function that is called in the background whenever a user signs the petition and opts in to the mailing list # Makes an API call to publicaster <--- More documentation to fo

我正在使用一个没有文档的API,我遇到了一个绊脚石。我有一个功能:

def add_to_publicaster(self):
    # function that is called in the background whenever a user signs the petition and opts in to the mailing list
    # Makes an API call to publicaster <--- More documentation to follow --->
    username = app.config['PUBLICASTER_USERID']
    userPass = app.config['PUBLICASTER_PASS']
    headers = {'Authorization': {username:userPass}, "Content-type" : "application/json", "Accept":'text/plain'}
    url = 'https://api7.publicaster.com/Rest/Subscribers.svc/1?format=json'
    data = {"Item": {
        "Email": "juliangindi@gmail.com"
        }
    }
    r = requests.post(url, headers = headers, data = data)
def添加到publicaster(self):
#每当用户签署请愿书并选择加入邮件列表时在后台调用的函数
#对PublicMaster进行API调用
username=app.config['PUBLICASTER\u USERID']
userPass=app.config['PUBLICASTER\u PASS']
headers={'Authorization':{username:userPass},“内容类型”:“application/json”,“Accept”:“text/plain”}
url='1〕https://api7.publicaster.com/Rest/Subscribers.svc/1?format=json'
数据={“项”:{
“电子邮件”:juliangindi@gmail.com"
}
}
r=requests.post(url,headers=headers,data=data)
这只是假设使用以下格式发出POST请求:

POST https://api7.publicaster.com/Rest/Subscribers.svc/1?format=json HTTP/1.1
Content-Type: application/json
Authorization: <AccountID>:<Password>
Host: api7.publicaster.com
Content-Length: 64
Expect: 100-continue
Connection: Keep-Alive
 { "Item" : {
  "Email" : mkucera@whatcounts.com
 }
}
POSThttps://api7.publicaster.com/Rest/Subscribers.svc/1?format=json HTTP/1.1
内容类型:application/json
授权::
主持人:api7.publicaster.com
内容长度:64
预期:100人继续
连接:保持活力
{“项目”:{
“电子邮件”:mkucera@whatcounts.com
}
}

但是,函数中的代码没有生成所需的请求。任何建议都会非常有用

您的标题和URL表明您想要发布JSON数据。使用
JSON
库将python结构编码为JSON:

import json

# ...

data = {"Item": {
    "Email": "juliangindi@gmail.com"
    }
}
r = requests.post(url, headers = headers, data = json.dumps(data))

JSON看起来很像Python,但它实际上是一种有限形式的JavaScript源代码。

您没有正确执行身份验证。您的函数应该如下所示:

def add_to_publicaster(self):
    # function that is called in the background whenever a user signs the petition and opts in to the mailing list
    # Makes an API call to publicaster <--- More documentation to follow --->
    username = app.config['PUBLICASTER_USERID']
    userPass = app.config['PUBLICASTER_PASS']
    headers = {"Content-type" : "application/json", "Accept":'text/plain'}
    url = 'https://api7.publicaster.com/Rest/Subscribers.svc/1?format=json'
    data = {"Item": {
        "Email": "juliangindi@gmail.com"
        }
    }
    r = requests.post(url, auth=(username, userPass), headers=headers, data=json.dumps(data))
def添加到publicaster(self):
#每当用户签署请愿书并选择加入邮件列表时在后台调用的函数
#对PublicMaster进行API调用
username=app.config['PUBLICASTER\u USERID']
userPass=app.config['PUBLICASTER\u PASS']
标题={“内容类型”:“应用程序/json”,“接受”:“文本/普通”}
url='1〕https://api7.publicaster.com/Rest/Subscribers.svc/1?format=json'
数据={“项”:{
“电子邮件”:juliangindi@gmail.com"
}
}
r=requests.post(url,auth=(用户名,userPass),headers=headers,data=json.dumps(数据))

您得到了什么?一条很长的错误消息,基本上是说服务器在处理请求时遇到了错误。我认为,解决方案是尽可能地匹配“sample”请求
json.dumps(dict(data=data))
不幸的是,我以前尝试过这个,但仍然得到一个错误。由于API的未记录性质,我可能只需要继续试验。@JulianGindi:你尝试的根本不起作用。确切的有效负载可能会有问题,但是如果您的标题说明您正在发布
application/json
数据,那么至少将数据编码为json。