Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I pass text/plain data to python's requests.post?

I am trying to convert this curl command in python requests, but I am unsure about how I should pass the data:

curl -X POST -u "apikey:{apikey}" \
    --header "Content-Type: text/plain" \
    --data "some plain text data" \
    "{url}"

I have tried to pass the string directly and to encode it with str.encode('utf-8') but I get an error 415 Unsupported Media Type

This is my code:

text = "some random text"
resp = requests.post(url, data=text, headers={'Content-Type': 'text/plain'}, auth=('apikey', self.apikey))

[EDIT]: Solution is not to specify the headers in the request

Thanks a lot!

like image 986
qmeeus Avatar asked Aug 12 '19 08:08

qmeeus


People also ask

How do you send data in a POST request in Python?

To send a POST request using the Python Requests Library, you should call the requests. post() method and pass the target URL as the first parameter and the POST data with the data= parameter.

How do you send data in requests?

You can use several HTTP methods in an HTTP request. Each method sends data in the request in a different manner. For example, the GET method uses the query string of the RequestURI to pass parameter and value pairs. Other methods use the HTTP message body to send data in the request.

How do I send a POST request with a payload in Python?

Set the Request Method: As the name suggests, we need to use a post method of a request module. Specify the POST data: As per the HTTP specification for a POST request, we pass data through the message body. Using requests, you'll pass the payload to the corresponding function's data parameter.

How do you pass payload in Python?

To send the JSON with payload to the REST API endpoint, you need to enclose the JSON data in the body of the HTTP request and indicate the data type of the request body with the "Content-Type: application/json" request header.


1 Answers

When using requests library, it is usually a good idea not to set Content-Type header manually using headers= keyword.

requests will set this header for you if it is needed (for example posting JSON will always result in Content-Type: application/json header).

Another reason for not setting this type of header manually is encoding, because sometimes you should specify something like Content-Type: text/plain; charset=utf-8.

One more important thing about Content-Type is that this header is not required for making POST requests. RFC 2616:

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".

So depending on the server type you're making request to, this header may be left empty.


Sorry for this explanation being a bit vague. I cannot give you an exact explanation why this approach worked for you unless you provide target URL.

like image 170
Ivan Vinogradov Avatar answered Sep 20 '22 12:09

Ivan Vinogradov