无法使用json文件发送post api请求

时间:2018-12-03 14:18:54

标签: python json python-3.x python-requests postman

我正在尝试使用python 3.7请求通过以下步骤在api服务器上发出发布请求:

  1. 加载json文件
  2. 调用request.post方法,并为data参数提供已加载的json文件。

使用这种方法,我从服务器收到“缺少所需的请求”响应。

但是,当我对Postman执行相同操作时,它运行起来很流畅。我也尝试从postman获取python代码,但给出相同的响应,即 缺少所需的请求 。这种行为令人惊讶,邮递员工作正常,但是当我尝试在python上复制邮递员时,出现此错误。

错误

{"error":"Internal Server Error","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing: public void com.netflix.spinnaker.gate.controllers.PipelineController.savePipeline(java.util.Map)","status":500,"timestamp":1543854206771}

request_post.py

import requests
url = "http://spinnaker-api.example.com/pipelines"
headers = {'Authorization': 'Bearer token','Accept' : 'application/json', 'Content-Type' : 'application/json'}
r = requests.post(url, data=open('example.json', 'rb'), headers=headers)
data=open(save_path + service_name + '/' + service_name + '.json', 'rb')
print(r.content)

example.json

{
 "application": "env",
 "expectedArtifacts": [
  {
   "defaultArtifact": {
    "kind": "custom",
    "name": "example-service_deployment.yaml",
    "reference": "https://example.com",
    "type": "http/file"
   },
   "matchArtifact": {
    "kind": "custom",
    "name": "example-service_deployment.yaml",
    "reference": "https://example.com",
    "type": "http/file"
   },
   "useDefaultArtifact": true,
   "usePriorExecution": false
  }
 ],
 "index": 4,
 "keepWaitingPipelines": false,
 "lastModifiedBy": "john.doe@example.com",
 "limitConcurrent": true,
 "name": "example-service",
 "stages": [
  {
   "account": "kubernetes",
   "cloudProvider": "kubernetes",
   "manifestArtifactAccount": "bitbucket-stash",
   "manifestArtifactId": "3240b93c-c980-4654-b391-136601d56679",
   "moniker": {
    "app": "env"
   },
   "name": "Deploy (Manifest)",
   "refId": "1",
   "relationships": {
    "loadBalancers": [],
    "securityGroups": []
   },
   "requisiteStageRefIds": [],
   "source": "artifact",
   "type": "deployManifest"
  }
 ],
 "triggers": [
  {
   "enabled": true,
   "expectedArtifactIds": [],
   "payloadConstraints": {
    "service": "example-service"
   },
   "source": "service-deployment",
   "type": "webhook"
  }
 ],
 "updateTs": "1541507182000"
}

邮递员生成的代码

import requests

url = "http://spinnaker-api.example.com/pipelines"

payload = "{\n \"application\": \"env\",\n \"expectedArtifacts\": [\n  {\n   \"defaultArtifact\": {\n    \"kind\": \"custom\",\n    \"name\": \"example-service_deployment.yaml\",\n    \"reference\": \"https://example.com\",\n    \"type\": \"http/file\"\n   },\n   \"matchArtifact\": {\n    \"kind\": \"custom\",\n    \"name\": \"example-service_deployment.yaml\",\n    \"reference\": \"https://example.com\",\n    \"type\": \"http/file\"\n   },\n   \"useDefaultArtifact\": true,\n   \"usePriorExecution\": false\n  }\n ],\n \"index\": 4,\n \"keepWaitingPipelines\": false,\n \"lastModifiedBy\": \"john.doe@example.com\",\n \"limitConcurrent\": true,\n \"name\": \"example-service\",\n \"stages\": [\n  {\n   \"account\": \"kubernetes\",\n   \"cloudProvider\": \"kubernetes\",\n   \"manifestArtifactAccount\": \"bitbucket-stash\",\n   \"manifestArtifactId\": \"3240b93c-c980-4654-b391-136601d56679\",\n   \"moniker\": {\n    \"app\": \"env\"\n   },\n   \"name\": \"Deploy (Manifest)\",\n   \"refId\": \"1\",\n   \"relationships\": {\n    \"loadBalancers\": [],\n    \"securityGroups\": []\n   },\n   \"requisiteStageRefIds\": [],\n   \"source\": \"artifact\",\n   \"type\": \"deployManifest\"\n  }\n ],\n \"triggers\": [\n  {\n   \"enabled\": true,\n   \"expectedArtifactIds\": [],\n   \"payloadConstraints\": {\n    \"service\": \"example-service\"\n   },\n   \"source\": \"service-deployment\",\n   \"type\": \"webhook\"\n  }\n ],\n \"updateTs\": \"1541507182000\"\n}"
headers = {
    'Content-Type': "application/json",
    'Authorization': "Bearer token",
    'cache-control': "no-cache",
    'Postman-Token': "postman-token"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

我研究了以下方法,但似乎都不起作用:

how to POST contents of JSON file to RESTFUL API with Python using requests module

how to POST contents of JSON file to RESTFUL API with Python using requests module

更新

因此邮递员还有一个附加的标头: 'cookie':"SESSION=1233647d3d-a91d-3ce6-af1d-b14cd7fde472"

将其添加到reques_post.py标头中,我能够获得所需的输出(与邮递员相同)。现在不知道为什么启用请求方法的会话cookie正常工作,为什么没有此标头也无法做到这一点。

0 个答案:

没有答案