[Vuejs]-How to use axios to send data to line notify

0👍

Oh I am too familiar with this. Heres an explanation on stackoverflow as to why your request works with postman but not in browser. Long story short browsers send a preflight options check that will ask the server if the action you’re about to perform is allowed. Postman does not. Usually the "Access-Control-Allow-Origin": "*" header is sent by the server to the browser not the other way around.

Inside the docs for LINE Notify you can find:

POST https://notify-api.line.me/api/notify
Sends notifications to users or groups that are related to an access token.

If this API receives a status code 401 when called, the access token will be deactivated on LINE Notify (disabled by the user in most cases). Connected services will also delete the connection information.

Requests use POST method with application/x-www-form-urlencoded (Identical to the default HTML form transfer type).

My guess is that your access_token might have been deactivated. Try requiring a new access token and doing the request again.

0👍

I think it is impossible to connect directly to the external url for the axios cuz ajax is basically for the inner data network. But you might have a controller if you do a web project, so you can just use your controller language to make a connection with line notify. In my case, I made a rails project and used axios in the vue.js, so I made a link like this.

View(axios) => Controller(Ruby) => LineAPI

0👍

me currently work on this too.

I did my app with node js.

My way below is for your reference, it works well.

const axios = require('axios');
const querystring = require('querystring');

axios({
    method: 'post',
    url: 'https://notify-api.line.me/api/notify',
    headers: {
      'Authorization': 'Bearer ' + 'YOUR_ACCESS_TOKEN',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Access-Control-Allow-Origin': '*'
    },
    data: querystring.stringify({
      message: 'something you would like to push',
    })
  })
.then( function(res) {
  console.log(res.data);
})
.catch( function(err) {
  console.error(err);
});

0👍

I try it works.

async function test() {
  const result = await axios({
    method: "post",
    url: "https://notify-api.line.me/api/notify",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": "Bearer [token]",

    },
    data: 'message=你好哇'
  })
    .then((response) => {
      console.log(response);
    })
    .catch((err) => {
      console.log(err);
    });
}

test();

I think you can check response on chrome debugger network.

enter image description here

or provide more information, thx.

Leave a comment