简体   繁体   English

通过POST请求发送数据

[英]Send data through POST request

I had been using sockets, with Python, for some time ago and I'm trying to understand why this POST which should send some data on fields data1 and data2 do not work. 一段时间以前,我一直在用Python使用套接字,而我试图理解为什么这个应该在字段data1和data2上发送一些数据的POST无法正常工作。

POST /method.php HTTP/1.1\\r\\nHost: localhost\\r\\nContent-Type: multipart/form-data\\r\\n\\r\\ndata1=something&data2= otherthing\\r\\n\\r\\n

What is the problem with this request? 此请求有什么问题?

Have you tried using the Requests library instead, example of a post request below 您是否尝试过使用Requests库,下面是一个发布请求的示例

import requests
header = {"Content-Type": "multipart/form-data"}
data1="something"
data2= "otherthing"

session_requests = requests.session()
result = session_requests.post("http://localhost/", data=dict(data1, data2), headers=header)

It might be easier to use the requests library so your code would look something like this: 使用请求库可能会更容易,因此您的代码应如下所示:

import requests

# Data
data = {
    'data1':'something', 
    'data2':'otherthing'
}

# Custom headers
headers = {
    'content-type': 'multipart/form-data'
}

# Get response from server
response = requests.post('http://localhost/', data=data, headers=headers)

# If you care about the response
print(response.json())

You can also send files and a whole lot of other stuff 您还可以发送文件许多其他内容

There are several things wrong with your request: 您的请求有几处错误:

 POST /method.php HTTP/1.1
 Host: localhost
 Content-Type: multipart/form-data

 data1=something&data2= otherthing

First, whenever a body is used within a HTTP request the length of the body must be known. 首先,无论何时在HTTP请求中使用主体,都必须知道主体的长度。 This is typically done by given the length up-front with Content-length in the HTTP header although also chunked encoding might be used if the full length is not known up front. 这通常是通过在HTTP标头中使用Content-length预先给定长度来完成的,尽管如果预先不知道完整长度,也可以使用分块编码。 Your request does not do any of these which means the request is an invalid HTTP request. 您的请求不执行任何这些操作,这意味着该请求是无效的HTTP请求。

Additionally you claim a Content-Type of multipart/form-data although your body is not of this type. 另外,尽管您的身体不是这种类型,但您声明了Content-Typemultipart/form-data With multipart/form-data your body would consist of several MIME parts separated by a text boundary and this boundary would need to have been declared in your Content-type header. 使用multipart/form-data您的正文将由几个MIME部分组成,这些MIME部分由文本边界分隔,并且此边界将需要在Content-type标头中声明。 The correct type for the body you show would be instead application/x-www-form-urlencoded . 您显示的正文的正确类型将改为application/x-www-form-urlencoded

Even with application/x-www-form-urlencoded the body is partly wrong. 即使使用application/x-www-form-urlencoded ,主体还是有部分错误。 This type of body should be only pairs of key=value concatenated by & , ie there should be neither as space after a key as you have after data2= nor there should be new lines added after the end of the data as you have. 这种类型的主体应该仅是由&链接的成对的key=value ,即,在data2=后面没有空格,而在data2=之后既没有空格,也没有在数据结尾之后添加新行。

When removing all these problems you should probably send the following request: 消除所有这些问题时,您可能应该发送以下请求:

body = "data1=something&data2=otherthing"
request = ("POST /method.php HTTP/1.1\r\n" + \
    "Host: localhost\r\n" + \
    "Content-Type: application/x-www-form-urlencoded\r\n" + \
    "Content-Length: %d\r\n" + \
    "\r\n%s") % (len(body),body)

But once you have send this request the trouble continues since getting the response correctly is complex too. 但是,一旦您发送了此请求,麻烦就继续了,因为正确获得响应也很复杂。 Generally I recommend to not code your own HTTP handling unless you really know what you do but instead use existing libraries. 通常,除非您真的知道自己要做什么,否则我建议不要编写自己的HTTP处理代码,而应使用现有的库。 While HTTP might look simple when just looking at a few example requests it is way more complex than it initially looks. 虽然仅看几个示例请求时HTTP看起来可能很简单,但它比最初看起来要复杂得多。 And while your code might seem to work against specific servers it might fail with other servers. 而且,尽管您的代码似乎可以在特定服务器上运行,但其他服务器可能无法运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM