2

my code is almost ready, but my friend has said it is running as a GET instead of as a POST. I don't see GET anywhere in this code, so how would I change it into a POST?

const request = require("request");

const username = "myUserName";
const password = "myPassword";
const options = {
  url: "https://siteToPostTo.com/api/v1/statuses",
  auth: {
    user: username,
    password: password
  },
  body: JSON.stringify({

    status: 'automated message to post'
  })
};

request(options, function(err, res, body) {
  if (err) {
    console.dir(err);
    return;
  }
  console.dir("headers", res.headers);
  console.dir("status code", res.statusCode);
  console.dir(body);
});

the cmd error message is "'headers' 'status code' '<"error":"Not implemented">'

EDIT: I tried adding method: "POST" in 3 different positions in the syntax but am still getting the same error

EDIT: almost working now, the error i am getting is " 'headers' 'status code' '<"errors":<"detail":"Internal Server Error">>' "

EDIT: okay, it seems like it is almost there, here is the last error "status code 400 <"errors":<"detail":"Internal server error">>

EDIT: in the body section, i was told to delete JSON.stringify and replace it with something else, but I can't figure out what

  • Possible duplicate of [How is an HTTP POST request made in node.js?](https://stackoverflow.com/questions/6158933/how-is-an-http-post-request-made-in-node-js) – blurfus Aug 15 '19 at 23:08
  • Short answer, add `method: 'POST'` to your options (GET is the default method) – blurfus Aug 15 '19 at 23:09
  • i tried adding method: "POST" in three different positions and it still didn't work and is giving me the same error – randomDev83746 Aug 15 '19 at 23:38

1 Answers1

4

According to request module documentation, GET is default HTTP method.

If you want to use POST method you should specify it in options object.

const request = require('request');

const username = 'myUserName';
const password = 'myPassword';
const options = {
    method: 'POST',
    url: 'https://siteToPostTo.com/api/v1/statuses',
    auth: {
        user: username,
        password: password
    },
    body: JSON.stringify({
        status: 'automated message to post'
    })
};

request(options, function(err, res, body) {
    if (err) {
        console.dir(err);
        return;
    }
    console.log('headers', res.headers);
    console.log('status code', res.statusCode);
    console.log(body);
});

Also, change console.dir() to console.log() (documentation). And you can check headers and status code in console.

Niklv
  • 136
  • 4
  • thank you, almost working now, the error i am getting is " 'headers' 'status code' '<"errors":<"detail":"Internal Server Error">>' " – randomDev83746 Aug 16 '19 at 00:06
  • @randomDev83746 it looks like you receive this error from `/api/v1/statuses` method. Please refer to `/api/v1/statuses` method documentation. – Niklv Aug 16 '19 at 00:19
  • thank you, it seemed to have gone through after i made the console.log change, but still got this error "status code 400 <"errors":<"detail":"Internal server error">> . Okay, let me check that documentation – randomDev83746 Aug 16 '19 at 00:22
  • on the documentation, it says "Scope write write:statuses", does that have anything to do with it? https://docs.joinmastodon.org/api/rest/statuses/#post-api-v1-statuses – randomDev83746 Aug 16 '19 at 00:26
  • @randomDev83746 Method `/api/v1/statuses` requires authentication. You should start with [authentication](https://docs.joinmastodon.org/api/authentication/) process. – Niklv Aug 16 '19 at 00:37
  • i was told to change the JSON.stringify in the body to something else, but I can't figure out what – randomDev83746 Aug 16 '19 at 12:15
  • @randomDev83746 at this point, the original question has been answered. Please post a separate question for the follow-up issues (since it's not related to POST/GET issue) – blurfus Aug 16 '19 at 12:50