简体   繁体   中英

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. Now when I send data using curl request

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. If there is a redirect how can I send data ?

SSLify issues a 301 or 302 redirect status code depending on your configuration. So you need to pass --post301 or --post302 to curl.

The reason for this can be found in the curl man page:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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