Curl statement `--form input = @. / Thefile.pdf` in python requests

I have a curl statement, as follows from this link :

curl -v -include --form input=@./thefile.pdf localhost:8080/processFulltextDocument

I am trying to use Requests to replicate the above statement in Python and therefore use the following code

    import requests
    Data = {'input': './samp.pdf'}
    url='http://127.0.0.1:8080/processFulltextDocument'
    r = requests.post(url,data=Data)
    print r.text

However, I get error 415. What am I doing wrong?

EDIT The curl operator headers are as follows:

curl -v -include --form input=@./samp.pdf 127.0.0.1:8080/processFulltextDocument

* Couldn't find host 127.0.0.1 in the .netrc file; using defaults
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> POST /processFulltextDocument HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:8080
> Accept: */*
> Content-Length: 549488
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------df1c59f42e57cbf4
> 
< HTTP/1.1 100 Continue
HTTP/1.1 100 Continue
+4
source share
2 answers

To send a POST request request "multipart / form-data", use the parameter files:

#!/usr/bin/env python
import requests  # $ pip install requests

r = requests.post('http://127.0.0.1:8080/processFulltextDocument',
                  files=dict(input=open('samp.pdf', 'rb')))
print(r.text) # print response

See the POST file with multi-page encoding .

+4
source

@ , ( ):

import requests
Data = {'input': open('./samp.pdf', 'rb')}

url='http://127.0.0.1:8080/processFulltextDocument'
r = requests.post(url,data=Data)
print r.text

2:

. r = requests.post(url,data=Data) r = requests.post(url,files=Data), . .

:

1: . , ( ):

import requests
Data = open('./samp.pdf', 'rb').read()

url='http://127.0.0.1:8080/processFulltextDocument'
r = requests.post(url,data=Data)
print r.text
+1

Source: https://habr.com/ru/post/1619732/


All Articles