0

I'm trying to make a POST request of multipart/form-data using mechanize, here's what it looks like from firefox live http header when I actually make a post:

http://example.com/new/example

POST /new/example HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://example.com/new/example
Cookie: tmgioct=c32MbAGn1sTuZrH8etPqVNU5; __qca=P0-495598852-1339139301054; __utma=189990958.911848588.1339139302.1339556345.1339561805.32; __utmz=189990958.1339139302.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); logged_in=1; tog_appearance_fieldset=fieldset_open; __utmc=189990958; pfu=42375294; pfp=h2YrFoaTr5LtrVys8PMmKNdyuoeA9FNLakxGzrJK; pfe=1371048319; __utmb=189990958.5.10.1339561805
Content-Type: multipart/form-data; boundary=---------------------------41184676334
Content-Length: 2947
-----------------------------41184676334
Content-Disposition: form-data; name="UPLOAD_IDENTIFIER"

0ad3af1c502c7cb59577b01720ee58ff014810c4
-----------------------------41184676334
Content-Disposition: form-data; name="post[state]"

2
-----------------------------41184676334
blahblahblahblah....

-----------------------------41184676334--

And here's my code:

browser = mechanize.Browser()

url = "http://example.com/new/example"
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0', 
    'Referer': 'http://example.com/new/example',
    'Content-Type': 'multipart/form-data; boundary=---------------------------41184676334'
}

data = "-----------------------------41184676334\rContent-Disposition: form-data; name="UPLOAD_IDENTIFIER"\r\r0ad3af1c502c7cb59577b01720ee58ff014810c4\r-----------------------------41184676334\rContent-Disposition: form-data; name="post[state]"\r\r2\r-----------------------------41184676334\rblahblahblahblah....\r\r-----------------------------41184676334--\r"

req = urllib2.Request(url, data, header)

response = browser.open(req, timeout = 30)
response.close()

I don't know why it does NOT work. Anybody knows? Please help me out.

By the way, does it have something to do with boundary? I use random numbers in above code.

3 Answers 3

1

From the MIME media types RFC 2046:

The canonical form of any MIME "text" subtype MUST always represent a line break as a CRLF sequence.

Your code uses carriage returns ('\r') only; you need to add line feeds (\n) as well.

Sign up to request clarification or add additional context in comments.

5 Comments

I've replaced all \r with \r\n and it still does NOT work, any other possible reason?
Define 'not work'. The website you are trying to spoof doesn't buy it? It could be that that site is looking for more than just the User-Agent and Referer headers to determine wether or not you use a 'real browser'.
Yeah, I guess so. Does it have something to do with boundary number? I simply use a random generated number.
I doubt it. Perhaps the some of the cookies in the Cookie header are important too?
Tried almost everything with the header part, but still cannot find out why it's not working. Will have to use a real browser for the job.
0
browser.form.enctype = "application/x-www-form-urlencoded"

Comments

0

Ended up using requests module to do the task. It turned out to be more convenient and reliable.

You can check out this page for details: POST a Multipart-Encoded File

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.