【问题标题】:Python requsts.post returning 405 error: The method is not allowed for the requested URLPython request.post 返回 405 错误:请求的 URL 不允许该方法
【发布时间】:2015-08-04 13:31:07
【问题描述】:

你好,

我只是在运行一个简单的烧瓶 API 调用。

flask API 将接收一个 XML 请求,然后解析 XML 并将其打印到终端屏幕。

但是,每次我这样做,我都会收到

请求的 URL 不允许该方法

Flask 脚本是:

__author__ = 'Jeremy'
from flask import Flask
from flask import request
import xmltodict

app = Flask(__name__)

@app.route('/', methods=['POST'])
def parsexml():

    xmlrequest = xmltodict.parse(request.data)

    print xmlrequest

if __name__ == '__main__':
    app.run()

发送 XML 的脚本是:

__author__ = 'Jeremy'

import requests

xml = """
<dtc:GetShipmentUpdates>
 <dtc:GetShipmentUpdatesRequest>
    <dtc:SearchStartTime>2015-07-12T12:00:00</dtc:SearchStartTime>
    <dtc:SearchEndTime>2015-07-12T12:30:00</dtc:SearchEndTime>
 </dtc:GetShipmentUpdatesRequest>
</dtc:GetShipmentUpdates> """

headers = {'Content-Type': 'application/xml'}
r = requests.post('http://127.0.0.1:5000/', data=xml, headers=headers)

print r.content

有谁知道为什么会这样,如果是这样,我如何向在 127.0.0.1:5000 上运行的烧瓶应用程序发送 POST 请求

【问题讨论】:

    标签: python xml


    【解决方案1】:

    您没有从parsexml 返回任何内容。尝试返回一些内容:

    @app.route('/', methods=['POST'])
    def parsexml():
        xmlrequest = xmltodict.parse(request.data)
        print xmlrequest
        return "Thanks for the data!"
    

    【讨论】:

      【解决方案2】:

      你好,

      您不能向 / 发送 POST 请求

      所以我将其更改为以下内容:

      __author__ = 'Jeremy'
      from flask import Flask
      from flask import request
      import xmltodict
      
      app = Flask(__name__)
      
      @app.route('/')
      def say_hello():
          return "Say goodbye Jeremy"
      
      @app.route('/api', methods=['POST'])
      def parsexml():
          xmlrequest = xmltodict.parse(request.data)
          return xmlrequest
      
      if __name__ == '__main__':
          app.run(host='0.0.0.0', port=int("80"))
      

      现在工作

      【讨论】:

      • 您可以向/发送POST请求。
      猜你喜欢
      • 2020-03-05
      • 1970-01-01
      • 2021-08-16
      • 2021-02-01
      • 2013-11-26
      • 2014-01-08
      • 2020-03-20
      • 2016-12-12
      • 2018-02-28
      相关资源
      最近更新 更多