简体   繁体   English

如何在 curl 重定向上发送数据?

[英]How to send data on a curl redirect?

I have an API which is currently on HTTP, I moved the API using SSLify library in python flask.我有一个当前在 HTTP 上的 API,我在 python Flask 中使用 SSLify 库移动了 API。 Now when I send data using curl request现在当我使用 curl 请求发送数据时

curl -v -k  -H "Content-Type: application/json" -X POST \
  --data '{"title":"foobar","body": "This body"}' \
  -L http://X.Y.Z.W.us-west-2.compute.amazonaws.com/test

It returns an empty string to me by using request.data If I make the request to begin with https it returns correct value.它通过使用 request.data 向我返回一个空字符串如果我请求以 https 开头,它将返回正确的值。 If there is a redirect how can I send data ?如果有重定向,我该如何发送数据?

SSLify issues a 301 or 302 redirect status code depending on your configuration. SSLify 根据您的配置发出 301 或 302 重定向状态代码。 So you need to pass --post301 or --post302 to curl.所以你需要通过--post301--post302来卷曲。

The reason for this can be found in the curl man page:原因可以在 curl 手册页中找到:

When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodified method.当 curl 跟随重定向并且请求不是普通的 GET(例如 POST 或 PUT)时,如果 HTTP 响应是 301、302 或 303,它将使用 GET 执行以下请求。如果响应代码是任何其他 3xx代码,curl 将使用相同的未修改方法重新发送以下请求。

You can tell curl to not change the non-GET request method to GET after a 30x response by using the dedicated options for that: --post301, --post302 and -post303.您可以使用专用选项告诉 curl 在 30x 响应后不要将非 GET 请求方法更改为 GET:--post301、--post302 和 -post303。

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

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