Error: options.uri is a required argument
Asked Answered
D

2

11

I'm using node.js v4.6.0 and the latest versions of express, request and body-parser, yet i'm getting an error code which I can't fix, any ideas?

Here's my code:

var express = require('express');
var request = require('request');
var bodyparser = require('body-parser');

var app = express();
app.use(bodyparser.urlencoded({extended: true}))

    var webhook = process.env.DISCORD_WEBHOOK;

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

app.post('/webhook', (req, res) =>{
    request({
        method: 'POST',
        url: webhook,
        json: {
            "content": req.body.msg,
            "username": "Potato"
        }
    });

    res.redirect("/");
});

app.listen(80, () => {
    console.log("Server Started!");
});

and the error message i'm receiving:

Error: options.uri is a required argument
   at Request.init (C:\Users\kingn\node_modules\request\request.js:233:31)
   at new Request (C:\Users\kingn\node_modules\request\request.js:129:8)
   at request (C:\Users\kingn\node_modules\request\index.js:55:10)
   at C:\Users\kingn\index.js:15:5
   at Layer.handle [as handle_request]     (C:\Users\kingn\node_modules\express\lib\router\layer.js:95:5)
   at next (C:\Users\kingn\node_modules\express\lib\router\route.js:131:13)
   at Route.dispatch     (C:\Users\kingn\node_modules\express\lib\router\route.js:112:3)
   at Layer.handle [as handle_request] (C:\Users\kingn\node_modules\express\lib\router\layer.js:95:5)
   at C:\Users\kingn\node_modules\express\lib\router\index.js:277:22
   at Function.process_params (C:\Users\kingn\node_modules\express\lib\router\index.js:330:12)

Any fixes?

Disconsolate answered 15/10, 2016 at 20:28 Comment(1)
in request method replace "url" with "uri"Panache
B
5

You need to provide URL information while making a request, check to see that correct URL is getting assigned to your webhook variable, i.e

var webhook = process.env.DISCORD_WEBHOOK; //webhook should be assigned a valid URL for example: 'https://stackoverflow.com/'

try console.log(webhook);

to find out URL for which you are making a request.

Boykin answered 21/4, 2017 at 2:2 Comment(1)
Please provide a little more detail on how the asker can trouble shoot the issue. Thanks for taking the time to answer this question.Uvula
P
-1

Object request has parameter uri not url. https://github.com/request/request#multipartrelated

 request({
    method: 'POST',
    uri: webhook,
    json: {
        "content": req.body.msg,
        "username": "Potato"
    }
});
Panache answered 15/10, 2016 at 20:36 Comment(2)
have you restarted the server?Panache
It should work with both uri and url. Make sure webhook is the full url with http:// or https://.Daugavpils

© 2022 - 2024 — McMap. All rights reserved.