Python Urllib/Urllib2 Post

Python URLLib / URLLib2 POST

u = urllib2.urlopen('http://myserver/inout-tracker', data)
h.request('POST', '/inout-tracker/index.php', data, headers)

Using the path /inout-tracker without a trailing / doesn't fetch index.php. Instead the server will issue a 302 redirect to the version with the trailing /.

Doing a 302 will typically cause clients to convert a POST to a GET request.

urllib2 - post request

Just to close the question:
The problem really was, that the server did not expect a POST requests (although it should, considered the use case). So (once again) the framework was not broken. ;)

python urllib2 post request

I am not sure It will work or not for you but check this piece of code

You can encode a dict using urllib like this:

import urllib
import urllib2

url = 'http://example.com/...'
values = { 'productslug': 'bar','qty': 'bar' }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
print result

Make an http POST request to upload a file using Python urllib/urllib2

After some digging around, it seems this post solved my problem. It turns out I need to have the multipart encoder setup properly.

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

register_openers()

with open("style.css", 'r') as f:
datagen, headers = multipart_encode({"file": f})
request = urllib2.Request("http://jigsaw.w3.org/css-validator/validator", \
datagen, headers)
response = urllib2.urlopen(request)

Making a POST call instead of GET using urllib2

This may have been answered before: Python URLLib / URLLib2 POST.

Your server is likely performing a 302 redirect from http://myserver/post_service to http://myserver/post_service/. When the 302 redirect is performed, the request changes from POST to GET (see Issue 1401). Try changing url to http://myserver/post_service/.

how to post data and binary data using urllib2 in python

import urllib2
import json

url = 'http://url.com?u=user&p=pass'
data = json.dumps({'config':'configData'}) # your JSON File goes in here, as argument to dumps.
cont_len = len(data)
req = urllib2.Request(url, data, {'Content-Type': 'application/json', 'Content-Length': cont_len})
f = urllib2.urlopen(req)
response = f.read()
f.close()

That solves it!

Note that, with urllib2, you cannot specify the .json file. You simply put its content into the json.dumps function.

Python urllib,urllib2 fill form

This is really more of something you would do with Selenium or similar. selenium.webdriver.common.action_chains.ActionChains.click, perhaps.

Python - make a POST request using Python 3 urllib

This is how you do it.

from urllib import request, parse
data = parse.urlencode(<your data dict>).encode()
req = request.Request(<your url>, data=data) # this will make the method "POST"
resp = request.urlopen(req)


Related Topics



Leave a reply



Submit