How to use the @hapi/wreck.post function in @hapi/wreck

To help you get started, we’ve selected a few @hapi/wreck 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 hapijs / bell / lib / oauth.js View on Github external
if (!settings.provider.useParamsAuth) {
            requestOptions.headers.Authorization = 'Basic ' + (Buffer.from(settings.clientId + ':' + settings.clientSecret, 'utf8')).toString('base64');
        }

        if (settings.provider.headers) {
            Hoek.merge(requestOptions.headers, settings.provider.headers);
        }

        if (typeof settings.clientSecret === 'object') {
            Hoek.merge(requestOptions, settings.clientSecret);
        }

        // Obtain token

        try {
            var { res: tokenRes, payload } = await Wreck.post(settings.provider.token, requestOptions);
        }
        catch (err) {
            return h.unauthenticated(Boom.internal('Failed obtaining ' + name + ' access token', err), { credentials });
        }

        if (tokenRes.statusCode < 200 ||
            tokenRes.statusCode > 299) {

            return h.unauthenticated(Boom.internal('Failed obtaining ' + name + ' access token', payload), { credentials });
        }

        try {
            payload = internals.parse(payload);
        }
        catch (err) {
            Bounce.rethrow(err, 'system');
github hapijs / hapi / test / payload.js View on Github external
it('returns an error on unsupported mime type', async () => {

        const server = Hapi.server();
        server.route({ method: 'POST', path: '/', handler: (request) => request.payload.key });
        await server.start();

        const options = {
            headers: {
                'Content-Type': 'application/unknown',
                'Content-Length': '18'
            },
            payload: '{ "key": "value" }'
        };

        const err = await expect(Wreck.post(`http://localhost:${server.info.port}/?x=4`, options)).to.reject();
        expect(err.output.statusCode).to.equal(415);
        await server.stop({ timeout: 1 });
    });
github hapijs / hapi / test / payload.js View on Github external
it('handles expect 100-continue', async () => {

        const server = Hapi.server();
        server.route({ method: 'POST', path: '/', handler: (request) => request.payload });

        await server.start();

        const uri = 'http://localhost:' + server.info.port;
        const { res, payload } = await Wreck.post(uri, { payload: { hello: true }, headers: { expect: '100-continue' } });
        expect(res.statusCode).to.equal(200);
        expect(payload.toString()).to.equal('{"hello":true}');

        await server.stop();
    });
github hapijs / hapi / test / transmit.js View on Github external
it('returns a gzip response on a post request when accept-encoding: deflate, gzip is requested', async () => {

            const data = '{"test":"true"}';
            const server = Hapi.server({ compression: { minBytes: 1 } });
            server.route({ method: 'POST', path: '/', handler: (request) => request.payload });
            await server.start();

            const uri = 'http://localhost:' + server.info.port;
            const zipped = await internals.compress('gzip', Buffer.from(data));
            const { payload } = await Wreck.post(uri, { headers: { 'accept-encoding': 'deflate, gzip' }, payload: data });
            expect(payload.toString()).to.equal(zipped.toString());
            await server.stop();
        });
github hapijs / pez / test / index.js View on Github external
server.once('listening', () => {

            port = server.address().port;

            const form = new FormData();
            form.append('file1', Fs.createReadStream('./test/files/file1.txt'));

            Wreck.post('http://127.0.0.1:' + port, {
                payload: form, headers: form.getHeaders()
            }, (err, res, payload) => {

                expect(err).to.not.exist();
            });
        });
github senecajs / seneca-transport / lib / http.js View on Github external
),
          'seneca-time-client-sent': data.time.client_sent
        }

        if (defaultHeaders) {
          headers = Object.assign(headers, defaultHeaders)
        }

        var requestOptions = {
          json: true,
          headers: headers,
          timeout: clientOptions.timeout,
          payload: JSON.stringify(data.act)
        }

        var postP = Wreck.post(url, requestOptions)

        postP
          .then(function(out) {
            handle_post(null, out.res, out.payload)
          })
          .catch(function(err) {
            handle_post(err)
          })

        function handle_post(err, res, payload) {
          var response = {
            kind: 'res',
            res: payload && 'object' === typeof payload ? payload : null,
            error: err,
            sync: (msg.meta$ || meta).sync
          }

@hapi/wreck

HTTP Client Utilities

BSD-3-Clause
Latest version published 7 months ago

Package Health Score

79 / 100
Full package analysis