【问题标题】:python HTTP POST fails with "only whitespace content allowed before start tag and not 1"python HTTP POST失败,“开始标签前只允许空白内容而不是1”
【发布时间】:2017-08-17 04:54:22
【问题描述】:

我在 python 中做 Python 客户端 HTTPSConnection.request('POST', url, data, headers)。 数据为b'1502944466046',即请求正文

调试打印显示:

send: b'POST /path/to/someresource HTTP/1.1\r\nHost: 172.1.2.3\r\nAccept-Encoding: identity\r\nContent-Length: 13\r\nContent-Type: application/ xml;charset=UTF-8\r\nAccept: application/xml\r\nAuthorization: Basic encodeduserPwd==\r\n\r\n'

发送:b'1502944466046'

在 Java 服务器上,我得到 org.xmlpull.v1.XmlPullParserException: only whitespace content allowed before start tag and not 1 (position: START_DOCUMENT seen 1...@1:1)

POST 的数据只是下面派生的“数据”变量,如下所示:

name = str(int(round(time.time() * 1000))) # time in millisec used
data = name.encode('utf-8')

Java REST 控制器有一个方法:

@RequestMapping(value=sameurlaspythonurlabove, method=RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void createMyObject(@RequestBody String name)

由于上述异常,该方法未命中。

正文不是 XML,但我认为服务器正试图从上面的错误中解析 XML。

如果我设置 Content-type = 'text/plain',那么服务器会给出 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported

任何想法如何规避?

【问题讨论】:

    标签: java python xml http-post xstream


    【解决方案1】:

    问题是您的代码需要 XML,但您给它的 JSON 格式以 1 开头。您可以将请求正文转换为 xml 格式,然后您可以尝试。

    【讨论】:

    • 是的,我刚刚成功实现了 封装。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-12-05
    • 2018-03-24
    • 2018-02-26
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    相关资源
    最近更新 更多