gpt4 book ai didi

c++ - 使用 HTTP 方法 POST 或 PUT 上传数据

转载 作者:行者123 更新时间:2023-11-30 16:14:48 25 4
gpt4 key购买 nike

我正在使用 STM32 微 Controller 和 C 语言,希望在我的网站上发送和接收数据。我可以通过以下代码从网站上使用“GET”方法接收 .txt 文件:

static const char http_request[] = "GET "WEBSITE_SUB_ADDRESS" HTTP/1.1\r\nHost: "WEBSITE_ADDRESS"\r\n\r\n";
net_sock_send(socket, (uint8_t *) http_request, len);
net_sock_recv(socket, (uint8_t *) buffer + read, NET_BUF_SIZE - read);

现在我想使用 http 方法(POST 或 PUT,...)将数据以文件形式发送或上传到网站。我该怎么做?

最佳答案

您首先需要决定是否要使用 POST 还是 PUT。

The PUT method completely replaces whatever currently exists at the target URL with something else. With this method, you can create a new resource or overwrite an existing one given you know the exact Request-URI. ...In short, the PUT method is used to create or overwrite a resource at a particular URL that is known by the client.

The HTTP POST method is used to send user-generated data to the web server. For example, a POST method is used when a user comments on a forum or if they upload a profile picture. A POST method should also be used if you do not know the specific URL of where your newly created resource should reside. ...In short, the POST method should be used to create a subordinate (or child) of the resource identified by the Request-URI.
from https://www.keycdn.com/support/put-vs-post

连接到服务器时,您将发送一个 HTTP header ,就像处理 GET 请求一样,如下所示:

POST /test HTTP/1.1\r\n
Host: www.myServer.com\r\n
Content-Type: text/plain\r\n
Content-Lenght: 8\r\n
Accept: */*\r\n
\r\n
someData

您可能还想通过查看发回给您的 header 来检查服务器是否收到消息,其中应包含 HTTP/1.1 200 OK

编辑:要将其放入文件中,请尝试 /test/mytext.txt 但我没有办法测试这是否有效

测试请求的好地方是 Post Test Server V2 。希望这有帮助

关于c++ - 使用 HTTP 方法 POST 或 PUT 上传数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57408647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com