Python 2 - 发布数据

时间:2018-06-18 11:49:07

标签: python json post request

我正在开发一个可以使用Curl的API,命令如下所示:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'api_key: someKey' -d '{
  "configurationPath": "/a/b",
  "entityTypesFilter": [
    "Filter1"
  ],  
  "pageSize": 10 
}' 'http://localhost:123/ab/rest/v1/entities'

如何将其转换为带有请求库的Python 2代码?

我尝试过类似的事情:

import requests
headers = {'api_key': 'someKey'}
url = "http://localhost:123/ab/rest/v1/entities"

data = {
  "configurationPath": "/a/b",
  "entityTypesFilter": [
    "Filter1"
  ],  
  "pageSize": 10 
}

r = requests.post(url, headers=headers, data = data)
print r.content

但是这给出了415错误:

> Status Report</p><p><b>Message</b> Unsupported Media Type</p><p><b>Description</b> The origin server is refusing to se
rvice the request because the payload is in a format not supported by this method on the target resource.</p><hr class="
line" /><h3>Apache Tomcat/9.0.8</h3></body></html>

如何解决?我认为它是data部分的格式,但不确定预期的内容以及如何修改以使其有效 感谢。

1 个答案:

答案 0 :(得分:2)

您的curl命令将数据作为JSON与application / json content-type一起发送;你的Python代码没有这样做。

如果您使用head参数代替a

请求将执行此操作

<form name="hellosign" method="post">
Name:<input type="text" name="name">
Phone: <input type="text" name="phone">
Email: <input type="text" name="email">
<input type="submit" value="submit">
</form>

<?php $client = new HelloSign\Client('62ae8b50d5293a72ae0aa31ef0f586d1fcf5c89707b9e68b906dd64864');
$request = new HelloSign\SignatureRequest;
$request->enableTestMode();
$request->setRequesterEmail('prasad@test.com');
$request->addFile('trxade.pdf');
$client_id = '0005fc6cf93ce7ce6116778555661';
$draft_request = new HelloSign\UnclaimedDraft($request, $client_id);
$draft_request->setIsForEmbeddedSigning(true);
$response = $client->createUnclaimedDraft($draft_request);
$claim_url = $draft_request->getClaimUrl();
foreach ($response as $key => $value) {
    if($key === 'signature_request_id')
    {
        echo $requestId =$value;echo "<br/>";   
    }

}
echo $requestId;
<script type="text/javascript" src="https://s3.amazonaws.com/cdn.hellosign.com/public/js/hellosign-embedded.LATEST.min.js"></script>
<script>
    HelloSign.init("0005fc6cf93ce7ce6116778555661");
    HelloSign.open({
    url: "<?= $claim_url;?>",
    allowCancel: true,
    skipDomainVerification:true 

});
</script>