From-data api create urllib2 in python - python

I have call so many api with the help of urllib2 json type. But now i want to crate from-data api with the help of urllib2 and it is not working
I have post api and url and this data
Dummy url = https://www.example.com/xyz?id=32323232
dummy data {'data': "here"}
data should be sent by form-data not raw json type
how can we write code in python with urllib2

url = https://www.example.com/xyz?id=32323232
data = {'data': "here"}
header = {'ContentType' : 'multipart/form-data'}
request = urllib2.Request(url, data, header)
response = urllib2.urlopen(request)
Refer : https://www.pythonforbeginners.com/python-on-the-web/how-to-use-urllib2-in-python/

Related

Python Post json API without Request

I am on a system without pip and its not planned to use.
Put I have do to a post with python to an API (With this post I want to add a row in a postgresdb)
it works with the following code. but there are two columns which are jsonb in postgres.
With this code these columns have quotation marks, so the json doesn't work properly.
How can I avoid this?
import urllib.parse
import urllib.request
def_post_wo_requ
json_post= { 'store' : 'Jack', 'store_detail' : {'Tel1':'00000','Tel2':'11111'}}
url=host+'/'+ pg_table
data = urllib.parse.urlencode(json_post)
data = data.encode('utf-8')
print(data)
The print data shows this:
b'store=Jack&store_detail=%7B%27Tel1%27%3A+%2700000%27%2C+%27Tel2%27%3A+%2711111%27%7D'
and the json in the db is this:
"{'Tel1': '00000', 'Tel2': '11111'}"
Do you need to convert the data to a string?
Would not this ("classic" approach) work ?
from urllib import request
import json
url = 'https://myapp.domain/endpoint'
json_data= {'store': 'Jack', 'store_detail': {'Tel1': '00000','Tel2': '11111'}}
req = request.Request(url, method="POST")
req.add_header('Content-Type', 'application/json')
data = json.dumps(data)
data = data.encode()
r = request.urlopen(req, data=data)

Getting response 403 from twitter using post requests

I am trying to automate tweets in Twitter using the python requests module but for some reason I'm not able to post anything and getting response 403 instead of 200 , here is the code that I'm using
post_url = 'https://twitter.com/i/api/CreateTweet'
headers = {"authorization":f"Bearer {tokens['token']}"}
data = {"tweet_text":'Hello world'}
r = requests.post(url=post_url ,headers=headers , data=data)
print(r)
I also tried to send a JSON object as data but still no luck
json_data = json.dumps(data)
r = requests.post(url=post_url ,headers=headers , json=json_data)
print(r)
EDIT
I am using the request URL from the headers

Python web scrape data request error

I'm trying to retrieve the response json data by a web site that I call.
The site is this:
WebSite DriveNow
On this page are shown on map some data. With browser debugger I can see the end point
end point
that sends response data json.
I have use this python to try scrape the json response data:
import requests
import json
headers = {
'Host': 'api2.drive-now.com',
'X-Api-Key': 'adf51226795afbc4e7575ccc124face7'
}
r = requests.get('https://api2.drive-now.com/cities/42756?expand=full', headers=headers)
json_obj = json.loads(r.content)
but I get this error:
hostname doesn't match either of 'activityharvester.com'
How I can retrieve this data?
Thanks
I have tried to call the endpoint that show json response using Postam, and passing into Header only Host and Api-Key. The result is the json that i want. But i i try the same call into python i recive the error hostname doesn't match either of 'activityharvester.com'
I don't understand your script, nor your question. Why two requests and three headers ? Did you mean something like this ?
import requests
import json
headers = {
'User-Agent': 'Mozilla/5.0',
'X-Api-Key':'adf51226795afbc4e7575ccc124face7',
}
res = requests.get('https://api2.drive-now.com/cities/4604?expand=full', headers=headers, allow_redirects=False)
print(res.status_code, res.reason)
json_obj = json.loads(res.content)
print(json_obj)

Python requests Post request data with Django

I'm trying to send a simple post request to a very simple django server and can't wrap my head around why the post data isn't appearing in the requests post dictionary and instead its in the request body.
Client code:
payload = {'test':'test'}
headers = {'Content-type': 'application/json','Accept': 'text/plain'}
url = "localhost:8000"
print json.dumps(payload)
r = requests.post(url,data=json.dumps(payload),headers=headers)
Server Code:
def submit_test(request):
if request.method == 'POST':
print 'Post: "%s"' % request.POST
print 'Body: "%s"' % request.body
return HttpResponse('')
What is printed out on the server is:
Post: "<QueryDict: {}>"
Body: "{"test": "test"}"
I've played around with the headers and sending the data as a straight dictionary and nothing seems to work.
Any ideas? Thanks!!
The POST dictionary only contains the form-encoded data that was sent in the body of the request. The body attribute contains the raw body of the request as a string. Since you are sending json-encoded data it only shows up in the raw body attribute and not in POST.
Check out more info in the docs.
Try form-encoded data and you should see the values in the POST dict as well:
payload = {'test':'test'}
url = "localhost:8000"
requests.post(url, data=payload)
Specifying a user-agent in the headers should enable Django to interpret the raw data of the body and to correctly populate the POST dictionary. The following should work:
payload = {'test': 'test'}
url = "http://localhost:8000"
headers = {'User-Agent': 'Mozilla/5.0'}
requests.post(url, data=payload, headers=headers)
You should remove 'Content-type' from headers and use default one which is 'multipart/form-data'
response = client.post(
'/some_url/',
data={'post_key': 'some_value'},
# content_type='application/json'
)
If you uncomment 'content_type' data will be only in request.body

How to do a x-http request (client) with Python

I am trying to reproduce a x-http request captured with Charles (Web Debugging Proxy) with Python but I can't find any documentation (or don't know what or where to look for).
I'd use the requests library for this, as it makes tasks like these easier.
The request you captured seems to be posting JSON data, albeit with a text/javascript content type:
import requests
import json
headers = {'Content-Type': 'text/javascript;charset=utf-8')
data = json.dumps({'mod': 'calendar.field', 'action': 'mini', 'vars': {"current": 0}})
r = requests.post('http://www.kavka.be/xhttp.mod', data=data, headers=headers)
where data is a JSON string created from the same information as your proxy-captured POST.
Alternatively, if you only want to use the standard library, use urllib2:
import urllib2
import json
headers = {'Content-Type': 'text/javascript;charset=utf-8')
data = json.dumps({'mod': 'calendar.field', 'action': 'mini', 'vars': {"current": 0}})
req = urllib2.Request('http://www.kavka.be/xhttp.mod', data, headers)
r = urllib2.urlopen(req)

Categories

Resources