4

I have a curl statement as follows taken 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 so am using 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 receive a 415 error. What am I doing wrong?

EDIT The headers of the curl statement 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
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Echchama Nayak
  • 971
  • 3
  • 23
  • 44
  • 3
    Using `@` for file names is specific to `curl`, see e.g. http://unix.stackexchange.com/questions/144479/what-does-the-at-symbol-before-a-filename-mean-in-a-curl-command – jonrsharpe Dec 13 '15 at 18:24
  • @jonrsharpe I removed the `@` but I still get an error – Echchama Nayak Dec 13 '15 at 18:27
  • then you're just posting the file name. If you don't understand the code you've copied, translating it will be near-impossible. – jonrsharpe Dec 13 '15 at 18:28
  • @jonrsharpe I also tried `{'input': open('./samp.pdf', 'rb')}` but still get an error – Echchama Nayak Dec 13 '15 at 18:31
  • 1
    For reference, assuming the HTTP error codes are being properly used, 415 is an "unsupported media type" HTTP error. – RyanWilcox Dec 13 '15 at 18:32
  • related: [How to send a “multipart/form-data” with requests in python?](http://stackoverflow.com/q/12385179/4279) – jfs Dec 13 '15 at 19:34

2 Answers2

4

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

#!/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 POST a Multipart-Encoded File.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

While the @ is unique to curl, you can do this in requests (which should work):

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

Update 2:

The code snippet is wrong. Change r = requests.post(url,data=Data) to r = requests.post(url,files=Data) and this will work. Copy paste error.

See also: Example on Requests website

Update 1: This will pass the file as a field in the post request. You can pass it as the body like so (I think):

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
RyanWilcox
  • 13,890
  • 1
  • 36
  • 60