I want to send a post request to a URL with specific data and header types. using these two links I found that how to do that, but it's not working:
https://stackoverflow.com/questions/5693931/python-post-request
How do I send a custom header with urllib2 in a HTTP Request?
This is my code:
import urllib
import urllib2
url = 'https://clients6.google.com/rpc'
values = [
{"method": "pos.plusones.get",
"id": "p",
"params": {
"nolog": True,
"id": "http://www.newswhip.com",
"source": "widget",
"userId": "@viewer",
"groupId": "@self"
},
"jsonrpc": "2.0",
"key": "p",
"apiVersion": "v1"
}]
headers = {"Content-type" : "application/json:"}
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
I can get result with these values in Postman Rest Client. but after execution of this code the result is:
Traceback (most recent call last):
File "D:/Developer Center/Republishan/republishan2/republishan2/test2.py", line 22, in <module>
data = urllib.urlencode(values)
File "C:\Python27\lib\urllib.py", line 1312, in urlencode
raise TypeError
TypeError: not a valid non-string sequence or mapping object
I've also tried to use dictionary instead of list like this:
values = {"method": "pos.plusones.get",
"id": "p",
"params": {
"nolog": True,
"id": "http://www.newswhip.com",
"source": "widget",
"userId": "@viewer",
"groupId": "@self"
},
"jsonrpc": "2.0",
"key": "p",
"apiVersion": "v1"
}
It executes the script but the result contains error:
{"error":{"code":-32700,"message":"Unable to parse json","data":[{"domain":"global","reason":"parseError","message":"Unable to parse json"}]}}
As I told, I can execute script with list instead of dictionary with Postman Rest Client.
Look at the results in Postman:
What should I do?