I am new to http requests and trying to automate some work. But I am unable to get the required result. I have looked many posts and documentation of python requests module but there is no change in the result.
Code I wrote
def installFont():
print "Installing font"
urlToHit = "some http address"
header_ = { "UserID": "00000", "PortalName": "EDC", "ModifyBy" : "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "Content-Type" : "application/json"}
body_ = {
"Email": "abc#xyz.com",
"AssetLicenseType": "Trial",
"MachineIds": ["machine1", "machine2"],
"fontAsset":
[
{
"FontId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"FontName": "Neue Aachen™ Pro Ultra Light",
"FontUrl": "http://helveticaurl",
"FontDownloadUrlAPI": "url",
"FontDownloadUrl" : "url1,
"FontFamilyName": "Neue Aachen™ Pro",
"FontFamilyUrl": "http://FontFamilyUrl",
"FontStyle": "Normal",
"FontWeight": "100",
"ExpiryDate": "2017-2-27 11:17:01",
"FontFamilyId": "34"
}
]
}
r = requests.request("POST", urlToHit, data=body_, headers=header_)
print r.headers
print r.status_code
print r.text
Itried same thing with postman which gives me correct result but via python I am getting output as
{'X-Processing-Time-Milliseconds': '3', 'Transfer-Encoding': 'chunked', 'X-Powered-By': 'ASP.NET', 'Server': 'Kestrel', 'Date': 'Mon, 06 Feb 2017 14:51:59 GMT', 'Content-Type': 'application/json'}
400
{"Message":"''"}
I think I am doing some mistake while passing body_ in
r = requests.request("POST", urlToHit, data=body_, headers=header_)
Output via postman
{"Message":"Created Successfully","SuccessCount":2,"FailCount":0}
You need to use:
r = requests.post(urlToHit, json=body_, headers=headers_)
Please go through the documentation.
Related
My audio for podcast and art for it on google drive, in doc says to make like this, but it returns 400 error
import requests
podcast_file = "https://drive.google.com/file/d/1w0IDXVotyHKEGSbBWUxEoDvaJQRsWCJO"
headers = {
'Authorization': 'Token here my token',
'Content-Type': 'application/json',
}
new_episode = {
"title":"Too many or too few?",
"description":"",
"summary":"",
"artist":"Muffin Man",
"tags":"",
"duration":23462,
"guid":"Buzzsprout788880",
"episode_number":1,
"season_number":1,
"explicit":False,
"private":False,
"email_user_after_audio_processed": False,
"audio_url": podcast_file,
"artwork_url": "https://drive.google.com/file/d/1xoNKAbTYX8GjVg0ZB_ekFUl8ljQICMZv"
}
url = 'https://www.buzzsprout.com/api/episodes.json'
req = requests.post(url, data=new_episode, headers=headers)
print(req.text)
print(req.status_code)
I would get access to this website, I get it from another website, https://www.betexplorer.com/soccer/algeria/ligue-1/bordj-bou-arreridj-tlemcen/tfvAHu7U/, in the Network section of Developer tools.
Code
import requests
url = 'https://www.betexplorer.com/archive-odds/4urejxv464x0xd4645/18/'
response = requests.get(url, headers={'User-Agent': 'my user agent'})
print(response)
Output <Response [404]>
Set Referer HTTP header to obtain correct response:
import json
import requests
url = "https://www.betexplorer.com/archive-odds/4urejxv464x0xd4645/18/"
headers = {
"Referer": "https://www.betexplorer.com",
}
data = requests.get(url, headers=headers).json()
print(json.dumps(data, indent=4))
Prints:
[
{
"date": "23.07. 17:36",
"odd": "1.88",
"change": "+0.08"
},
{
"date": "23.07. 17:34",
"odd": "1.80",
"change": "-0.01"
},
...
I am new to rest api. I am trying to post data to service. recently I am doing it through postman tool and it's working.
Now I want to do it using python so I am copying postman's python code it's working, but is there any other way to send data using python
like here my python script
import requests
url = "http://http:/localhost:3200/api/log"
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ip\"\r\n\r\n235.23.14.242\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n{\n\t\t\"Main\": \n\t\t{\n\t\t \"subfolder\" : \n\t\t\t{ \n\t\t\t \"photos\" : \n\t\t\t\t{\n\t\t\t\t \"January\" : \n\t\t\t\t\t[\n\t\t\t\t\t\t\"name Detail of photo\",\n\t\t\t\t\t\t\"date id of photo\",\n\t\t\t\t\t\t\"location location detail\"\n\t\t\t\t\t],\n\t\t\t\t \"February\" : \n\t\t\t\t\t[\n\t\t\t\t\t\t\"name Detail of photo\",\n\t\t\t\t\t\t\"date id of photo\",\n\t\t\t\t\t\t\"location location detail\"\n\t\t\t\t\t]\n\t\t\t\t}\n}\n}\n}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'cache-control': "no-cache",
'Postman-Token': "5466e12e-b5d8-4326-a75c-8c9502963ed5"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
this code is working but here I have to put json data in payload . can I do it other way ?? like load json file in string and pass that string in payload . Actually I tried it but it's not working like this
import requests
url = "http://http:/localhost:3200/api/log"
str="235.23.14.242"
files={
"Main":
{
"subfolder" :
{
"photos" :
{
"January" :
[
"name Detail of photo",
"date id of photo",
"location location detail"
],
"February" :
[
"name Detail of photo",
"date id of photo",
"location location detail"
]
}
}
}
}
payload = {"ip":str,"file":files}
headers = {
'content-type': "multipart/form-data; boundary=---- WebKitFormBoundary7MA4YWxkTrZu0gW",
'cache-control': "no-cache",
'Postman-Token': "5466e12e-b5d8-4326-a75c-8c9502963ed5"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
Is there any other way to send this type of data ???
finally I found ans...
import requests
url = "http://http:/localhost:3200/api/log"
strr="235.23.14.242"
files={
"Main":
{
"subfolder" :
{
"photos" :
{
"January" :
[
"name Detail of photo",
"date id of photo",
"location location detail"
],
"February" :
[
"name Detail of photo",
"date id of photo",
"location location detail"
]
}
}
}
}
fl=str(files)
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; " \
"name=\"ip\"\r\n\r\n"+strr+"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; " \
"name=\"file\"\r\n\r\n"+fl+"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=---- WebKitFormBoundary7MA4YWxkTrZu0gW",
'cache-control': "no-cache",
'Postman-Token': "5466e12e-b5d8-4326-a75c-8c9502963ed5"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
Thanks for response..
I am new to python & POST message.
I am trying to call an API with JSON POST message and expecting a JSON response but my initial code not able to make the call as required .
using the URL,Headers & Postdata in chrome browser POST extension works fine.
#!/usr/bin/python
import requests
import json
url = 'http://xxxxx:111/batches'
postdata = {
"active": "true",
"size": "2",
"ctr": {
"user": "Admin",
"id": "1234"}}
#headers = {'content-type': 'application/json',
#'Authorization': 'Basic xyz879jjkhhnm',
#'Accept-Encoding': '0'}
headers = {'Authorization': 'Basic xyz879jjkhhnm', 'Accept-Encoding': '0'}
print headers
post_call = requests.post(url, headers=headers, data=json.dumps(postdata))
print post_call, "POST call"
print post_call.text, "TEXT"
print post_call.content, "CONTENT"
post_call.status_code, "STATUS CODE"
Error:
{'Accept-Encoding': '0', 'Authorization': 'Basic xyz879jjkhhnm'}
<Response [500]> POST call
[{"code":"server_error","description":"com.sun.jersey.api.MessageException: A message body reader for Java class com.hide.cpn.rest.v1.entity.CouponCodeBatchResourceEntity,
and Java type class com.hide.cpn.rest.v1.entity.CouponCodeBatchResourceEntity, and MIME media type application/octet-stream was not found.
\nThe registered message body readers compatible with the MIME media type are:\napplication/octet-stream ->\n
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider\n com.sun.jersey.core.impl.provider.entity.FileProvider\n
com.sun.jersey.core.impl.provider.entity.InputStreamProvider\n com.sun.jersey.core.impl.provider.entity.DataSourceProvider\n
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider\n*/* ->\n com.sun.jersey.core.impl.provider.entity.FormProvider\n
com.sun.jersey.core.impl.provider.entity.StringProvider\n com.sun.jersey.core.impl.provider.entity.ByteArrayProvider\n
com.sun.jersey.core.impl.provider.entity.FileProvider\n com.sun.jersey.core.impl.provider.entity.InputStreamProvider\n
com.sun.jersey.core.impl.provider.entity.DataSourceProvider\n com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General\n
com.sun.jersey.core.impl.provider.entity.ReaderProvider\n com.sun.jersey.core.impl.provider.entity.DocumentProvider\n
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader\n com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader\n
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader\n com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General\n
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General\n com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General\n
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General\n com.sun.jersey.core.impl.provider.entity.EntityHolderReader\n
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General\n com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General\n
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy\n","errorValues":null}] TEXT
**Edit:1 (changing Headers seems to get ridoff Java error now ,but getting below error now)**
headers = {'Authorization': 'Basic xyz879jjkhhnm', 'Accept-Encoding': '0', 'content-type': 'application/json'}
New error:
{'content-type': 'application/json', 'Accept-Encoding': '0', 'Authorization': 'Basic xyz879jjkhhnm'}
<Response [400]> POST call
[{"code":"invalid_attribute_of_request","description":"Attribute value type is not Integer. actual value = (\"2\")","errorValues":null,"field":"size"}] TEXT
[{"code":"invalid_attribute_of_request","description":"Attribute value type is not Integer. actual value = (\"2\")","errorValues":null,"field":"size"}] CONTENT
Ok,as per the latest change Header change in (EDIT 1) i had to remove the double quotes from post data "SIZE",that solved my error.
headers = {'Authorization': 'Basic xyz879jjkhhnm', 'Accept-Encoding': '0', 'content-type': 'application/json'}
postdata = {
"active": "true",
"size": "2", --> Changed to "size": 2,
"ctr": {
"user": "Admin",
"id": "1234"}}
I’m trying to get the header from a website, encode it in JSON to write it to a file.
I’ve tried two different ways without success.
FIRST with urllib2 and json
import urllib2
import json
host = ("https://www.python.org/")
header = urllib2.urlopen(host).info()
json_header = json.dumps(header)
print json_header
in this way I get the error:
TypeError: is not
JSON serializable
So I try to bypass this issue by converting the object to a string -> json_header = str(header)
In this way I can json_header = json.dumps(header) but the output it’s weird:
"Date: Wed, 02 Jul 2014 13:33:37 GMT\r\nServer: nginx\r\nContent-Type:
text/html; charset=utf-8\r\nX-Frame-Options:
SAMEORIGIN\r\nContent-Length: 45682\r\nAccept-Ranges: bytes\r\nVia:
1.1 varnish\r\nAge: 1263\r\nX-Served-By: cache-fra1220-FRA\r\nX-Cache: HIT\r\nX-Cache-Hits: 2\r\nVary: Cookie\r\nStrict-Transport-Security:
max-age=63072000; includeSubDomains\r\nConnection: close\r\n"
SECOND with requests
import requests
r = requests.get(“https://www.python.org/”)
rh = r.headers
print rh
{'content-length': '45682', 'via': '1.1 varnish', 'x-cache': 'HIT',
'accept-ranges': 'bytes', 'strict-transport-security':
'max-age=63072000; includeSubDomains', 'vary': 'Cookie', 'server':
'nginx', 'x-served-by': 'cache-fra1226-FRA', 'x-cache-hits': '14',
'date': 'Wed, 02 Jul 2014 13:39:33 GMT', 'x-frame-options':
'SAMEORIGIN', 'content-type': 'text/html; charset=utf-8', 'age':
'1619'}
In this way the output is more JSON like but still not OK (see the ‘ ‘ instead of “ “ and other stuff like the = and ;).
Evidently there’s something (or a lot) I’m not doing in the right way.
I’ve tried to read the documentation of the modules but I can’t understand how to solve this problem.
Thank you for your help.
There are more than a couple ways to encode headers as JSON, but my first thought would be to convert the headers attribute to an actual dictionary instead of accessing it as requests.structures.CaseInsensitiveDict
import requests, json
r = requests.get("https://www.python.org/")
rh = json.dumps(r.headers.__dict__['_store'])
print rh
{'content-length': ('content-length', '45474'), 'via': ('via', '1.1
varnish'), 'x-cache': ('x-cache', 'HIT'), 'accept-ranges':
('accept-ranges', 'bytes'), 'strict-transport-security':
('strict-transport-security', 'max-age=63072000; includeSubDomains'),
'vary': ('vary', 'Cookie'), 'server': ('server', 'nginx'),
'x-served-by': ('x-served-by', 'cache-iad2132-IAD'), 'x-cache-hits':
('x-cache-hits', '1'), 'date': ('date', 'Wed, 02 Jul 2014 14:13:37
GMT'), 'x-frame-options': ('x-frame-options', 'SAMEORIGIN'),
'content-type': ('content-type', 'text/html; charset=utf-8'), 'age':
('age', '1483')}
Depending on exactly what you want on the headers you can specifically access them after this, but this will give you all the information contained in the headers, if in a slightly different format.
If you prefer a different format, you can also convert your headers to a dictionary:
import requests, json
r = requests.get("https://www.python.org/")
print json.dumps(dict(r.headers))
{"content-length": "45682", "via": "1.1 varnish", "x-cache": "HIT",
"accept-ranges": "bytes", "strict-transport-security":
"max-age=63072000; includeSubDomains", "vary": "Cookie", "server":
"nginx", "x-served-by": "cache-at50-ATL", "x-cache-hits": "5", "date":
"Wed, 02 Jul 2014 14:08:15 GMT", "x-frame-options": "SAMEORIGIN",
"content-type": "text/html; charset=utf-8", "age": "951"}
If you are only interested in the header, make a head request. convert the CaseInsensitiveDict in a dict object and then convert it to json.
import requests
import json
r = requests.head('https://www.python.org/')
rh = dict(r.headers)
json.dumps(rh)
import requests
import json
r = requests.get('https://www.python.org/')
rh = r.headers
print json.dumps( dict(rh) ) # use dict()
result:
{"content-length": "45682", "via": "1.1 varnish", "x-cache": "HIT", "accept-ranges": "bytes", "strict-transport-security": "max-age=63072000; includeSubDomains", "vary": "Cookie", "server": "nginx", "x-served-by": "cache-fra1224-FRA", "x-cache-hits": "5", "date": "Wed, 02 Jul 2014 14:08:04 GMT", "x-frame-options": "SAMEORIGIN", "content-type": "text/html; charset=utf-8", "age": "3329"}
I know this is an old question, but I stumbled across it when trying to put together a quick and dirty Python curl-esque URL getter. I kept getting an error:
TypeError: Object of type 'CaseInsensitiveDict' is not JSON serializable
The above solutions are good if need to output a JSON string immediately, but in my case I needed to return a python dictionary of the headers, and I wanted to normalize the capitalization to make all keys lowercase.
My solution was to use a dict comprehension:
import requests
response = requests.head('https://www.python.org/')
my_dict = {
'body': response.text,
'http_status_code': response.status_code,
'headers': {k.lower(): v for (k, v) in response.headers.items()}
}