0

I am trying to POST JSON data to a api using a python script. I'm using python 2 and mechanize package.

import mechanize
import json, logging, sys, urllib

br = mechanize.Browser()
url = 'url_here'
headers = [('User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0'),
('Content-Type', 'application/json')]
br.set_handle_robots(False)
br.set_debug_responses(True)
br.set_debug_redirects(True)
br.set_debug_http(True)
logger = logging.getLogger("mechanize")
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)
body = {"audience":{"name":"asd","criteria":[{"category":"common","label":"Industry","tags":[{"tag":"Information Technology","text":"Information Technology","score":2,"type":"Similar"},{"tag":"Computer Software","text":"Computer Software","score":2,"type":"Similar"},{"tag":"Marketing and Advertising","text":"Marketing and Advertising","score":2,"type":"Similar"}],"type":"input-group","icon":"restaurant_menu","operation":"include","suggestions":True,"related":False,"multiple":True,"rankings":True,"showStrictness":True,"strict":False,"stub":{"tags":[],"values":[]},"values":["Information Technology","Computer Software","Marketing and Advertising"],"field":"industry","scope":"companies"},{"category":"common","label":"Contact Location","type":"location-group","icon":"person_pin_circle","operation":"include","multiple":True,"suggestions":True,"related":False,"tip":"Find people based on their location. You can filter by Country, and Region / City (for example, San Francisco Bay Area).","options":[{"value":"country","label":"Country","placeholder":"e.g... United States, Australia"},{"value":"locality","label":"Region / City","placeholder":"e.g... San Francisco, Toronto"}],"stub":{"tags":[],"values":[]},"tags":[],"values":["India","United States of America","United Kingdom"],"field":"country","childField":"country","scope":"contacts"},{"category":"tech","label":"Technology Used","type":"input-group","operation":"include","suggestions":True,"related":False,"icon":"power","multiple":True,"rankings":True,"placeholder":"e.g... Salesforce, jQuery, Ruby","tip":"Find companies based on specific web-based technologies they use. Note: This filter will narrow your search significantly. Any companies that dont use the technology you enter will be excluded from the results. ","stub":{"tags":[],"values":[]},"tags":[{"tag":"jQuery","text":"jQuery","score":2,"type":"Similar"},{"tag":"Angular JS v2","text":"Angular JS v2","score":0,"type":"Similar"}],"values":["jQuery","Angular JS v2"],"field":"technology","scope":"companies"}],"purchasedLeads":0,"estimatedReach":0,"maxLeadsPerCompany":3,"searchLeadCount":500,"emailGuesserScope":"professional"}}
br.addheaders = headers
response = br.open(url, data=json.dumps(body))
print response

But I am unable to send the JSON data successfully. But when I'm sending the data using urllib.urlencode() then the data is being sent but not being processed in the server as the server is expecting JSON data through the post request. Please someone help with this.

6
  • 2
    Does it have to be mechanize? requests can make this really trivial if you want to explore that avenue. Commented Jun 18, 2018 at 17:20
  • Can I use requests to bypass robots.txt? and add headers? Commented Jun 18, 2018 at 17:21
  • 1
    You can set the headers to bypass robots.txt, yes. Commented Jun 18, 2018 at 17:33
  • Can you help me with that then?? Commented Jun 18, 2018 at 18:12
  • 1
    Post the URL. Can't be doing some guessing here. Commented Jun 18, 2018 at 19:20

1 Answer 1

2

Okay so decided to go with the requests module as suggested by @Manhattan. It's much easier and simple to send post request with json data using requests.

r = requests.post(url, json={"audienceId":id,"leadCount":500}, headers=h)

Here the json parameter is just a python dictionary. and headers is also a dictionary. Official documentation

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

1 Comment

Above is also true for sending raw data using Body (similar like Postman POST request with params sent via Body and raw type).

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.