How to use requests - 10 common examples

To help you get started, we’ve selected a few requests examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kmyk / online-judge-tools / tests / command_submit.py View on Github external
int a; cin >> a;
            int b, c; cin >> b >> c;
            string s; cin >> s;
            cout << a + b + c << ' ' << s << endl;
            return 0;
        }
        '''
        files = [
            {
                'path': 'main.cpp',
                'data': code
            },
        ]

        with tests.utils.sandbox(files):
            with self.assertRaises(requests.exceptions.HTTPError):
                args = ["submit", '-y', '--no-open', url, 'main.cpp']
                args = get_parser().parse_args(args=args)
                submit(args)
github guyskk / flask-restaction / tests / test_cli.py View on Github external
def test_url_prefix():
    url = "http://127.0.0.1:5000"
    meta = requests.get(url, headers={'Accept': 'application/json'}).json()
    url_prefix, __, __ = parse_meta(meta)
    assert url_prefix == ""
github beaker-project / beaker / IntegrationTests / src / bkr / inttest / labcontroller / test_proxy.py View on Github external
def test_POST_pass(self):
        results_url = '%srecipes/%s/tasks/%s/results/' % (self.get_proxy_url(),
                self.recipe.id, self.recipe.tasks[0].id)
        response = requests.post(results_url, data=dict(result='Pass',
                path='/random/junk', score='123', message='The thing worked'))
        self.assertEquals(response.status_code, 201)
        self.assert_(response.headers['Location'].startswith(results_url),
                response.headers['Location'])
        result_id = int(posixpath.basename(response.headers['Location']))
        self.check_result(result_id, TaskResult.pass_, u'/random/junk', 123,
                u'The thing worked')
github ztgrace / changeme / tests / changeme_tests.py View on Github external
    @responses.activate
    def test_check_basic_auth_tomcat_fail(self):
        responses.add(** mock.tomcat_fp)
        responses.add(** mock.tomcat_fp_alt)

        cred = self.get_cred(self.tomcat_name)
        assert cred['name'] == self.tomcat_name

        changeme.logger = changeme.setup_logging(False, False, None)
        s = requests.Session()
        matches = changeme.check_basic_auth(mock.tomcat_fp['url'], s, cred, self.config, False, False)
        assert len(matches) == 0
github rusoto / rusoto / integration_tests / docker_test_run.py View on Github external
def _wait_for_s3(self):
        while True:
            time.sleep(1)
            try:
                requests.get("http://localhost:{}".format(self.port))
                break
            except requests.exceptions.ConnectionError:
                print("waiting for container to become ready", self)

        print("container ready, waiting another 5 seconds to ensure everything is set up")
        time.sleep(5)
github haiwen / seafile / integration-tests / autosetup.py View on Github external
def create_test_user(cfg):
    data = {'username': ADMIN_USERNAME, 'password': ADMIN_PASSWORD, }
    res = requests.post(apiurl('/auth-token/'), data=data)
    debug('%s %s', res.status_code, res.text)
    token = res.json()['token']
    data = {'password': PASSWORD, }
    headers = {'Authorization': 'Token ' + token}
    res = requests.put(
        apiurl('/accounts/{}/'.format(USERNAME)),
        data=data,
        headers=headers)
    assert res.status_code == 201
github ridi / lightweight-rest-tester / rest_tester / function / __init__.py View on Github external
def _send_request(method, url, params, data, **settings):
        if method == TestMethod.GET:
            return requests.get(url=url, params=params, **settings)
        elif method == TestMethod.PUT:
            return requests.put(url=url, data=data, **settings)
        elif method == TestMethod.POST:
            return requests.post(url=url, data=data, **settings)
        elif method == TestMethod.PATCH:
            return requests.patch(url=url, data=data, **settings)
        elif method == TestMethod.DELETE:
            return requests.delete(url=url, **settings)
        else:
            raise UnsupportedMethodError('Unsupported method: %s' % method)
github opendaylight / transportpce / tests / transportpce_tests / 1.2.1 / test_renderer_service_path_nominal.py View on Github external
def test_03_rdm_portmapping(self):
        url = ("{}/config/transportpce-portmapping:network/"
               "nodes/ROADMA01"
               .format(self.restconf_baseurl))
        headers = {'content-type': 'application/json'}
        response = requests.request(
             "GET", url, headers=headers, auth=('admin', 'admin'))
        self.assertEqual(response.status_code, requests.codes.ok)
        res = response.json()
        self.assertIn(
             {'supporting-port': 'L1', 'supporting-circuit-pack-name': '2/0',
              'logical-connection-point': 'DEG1-TTP-TXRX', 'port-direction': 'bidirectional'},
             res['nodes'][0]['mapping'])
        self.assertIn(
             {'supporting-port': 'C7', 'supporting-circuit-pack-name': '4/0',
              'logical-connection-point': 'SRG1-PP7-TXRX', 'port-direction': 'bidirectional'},
             res['nodes'][0]['mapping'])
github opendaylight / transportpce / tests / transportpce_tests / 1.2.1 / test_topology.py View on Github external
#Delete in the topology-netconf
        url = ("{}/config/network-topology:"
                "network-topology/topology/topology-netconf/node/ROADMC01"
               .format(self.restconf_baseurl))
        data = {}
        headers = {'content-type': 'application/json'}
        response = requests.request(
             "DELETE", url, data=json.dumps(data), headers=headers,
             auth=('admin', 'admin'))
        self.assertEqual(response.status_code, requests.codes.ok)
        #Delete in the clli-network
        url = ("{}/config/ietf-network:networks/network/clli-network/node/NodeC"
               .format(self.restconf_baseurl))
        data = {}
        headers = {'content-type': 'application/json'}
        response = requests.request(
             "DELETE", url, data=json.dumps(data), headers=headers,
             auth=('admin', 'admin'))
        self.assertEqual(response.status_code, requests.codes.ok)
github home-assistant / home-assistant / tests / components / test_api.py View on Github external
headers=HA_HEADERS)
        self.assertEqual(422, req.status_code)

        # Setup a real one
        req = requests.post(
            _url(const.URL_API_EVENT_FORWARD),
            data=json.dumps({
                'api_password': API_PASSWORD,
                'host': '127.0.0.1',
                'port': SERVER_PORT
                }),
            headers=HA_HEADERS)
        self.assertEqual(200, req.status_code)

        # Delete it again..
        req = requests.delete(
            _url(const.URL_API_EVENT_FORWARD),
            data=json.dumps({}),
            headers=HA_HEADERS)
        self.assertEqual(400, req.status_code)

        req = requests.delete(
            _url(const.URL_API_EVENT_FORWARD),
            data=json.dumps({
                'host': '127.0.0.1',
                'port': 'abcd'
                }),
            headers=HA_HEADERS)
        self.assertEqual(422, req.status_code)

        req = requests.delete(
            _url(const.URL_API_EVENT_FORWARD),