0

So, I'm programming a discord bot that basically conatcts an API I made for a whole bunch of different commands. Unfortunately, I am stuck on this one put request and I have the error pinpointed. The request doesn't seem to be sending the auth key that I have set in the headers. I checked the APIs logs and the auth middleware rejects the request because no key is provided. I would appreciate any and all help.

  • This is in an async function
  • All other requests have worked, in fact I copied and pasted this header from a previous working one
  • This request requires no body
  • In case this is somewhat relevant: This is in a collector.on('collect', async () => {HERE}).
let updatedUser = await axios.put(
                            `https://API-URL-GOES-HERE/api/users/${username}/loaChange`,
                            {
                                headers: {
                                    auth:
                                        'AUTH-KEY-GOES-HERE'
                                }
                            }
                        );

RECIEVED HEADERS FROM FAILURE:

{
   host: 'API-URL-HERE.herokuapp.com',
   connection: 'close',
   accept: 'application/json, text/plain, */*',
   'content-type': 'application/json;charset=utf-8',
   'user-agent': 'axios/0.19.0',
   'x-request-id': 'REQID',
   'x-forwarded-for': 'IPADRESS',
   'x-forwarded-proto': 'https',
   'x-forwarded-port': '443',
   via: '1.1 vegur',
   'connect-time': '2',
   'x-request-start': '1577766826940',
   'total-route-time': '5',
   'content-length': '194'
 }

PREVIOUS SUCCESFUL REQUEST:

let initialUser = await axios.get(
                        `https://API-URL-GOES-HERE/api/users/${username}`,
                        {
                            headers: {
                                auth:
                                    'AUTH-KEY-GOES-HERE'
                            }
                        }
                    );

Recieved Headers from Success:

{
host: 'API-URL-HERE.herokuapp.com',
  connection: 'close',
   accept: 'application/json, text/plain, */*', 
  auth: 'AUTH-KEY-HERE',
   'user-agent': 'axios/0.19.0',
   'x-request-id': 'REQID',
   'x-forwarded-for': 'IPADDRESS',
  'x-forwarded-proto': 'https',
 'x-forwarded-port': '443',
   via: '1.1 vegur',
   'connect-time': '0',
   'x-request-start': '1577766773203',
   'total-route-time': '0'
 }

NOTE: THIS IS NOT USING THE FORMAT OF A REGULAR AUTHENTICATION REQUEST, JUST THINK OF IT AS A HEADER OBJECT THAT ISN'T BEING PASSED THROUGH

Jordy337
  • 390
  • 2
  • 5
  • 17
  • Does this answer? https://stackoverflow.com/questions/44245588/how-to-send-authorization-header-with-axios – Siva K V Dec 31 '19 at 04:26
  • Please capture the request headers from an actual successful request (in the other code you have that is working). Then capture what this axios request sends. We need to see exactly what the headers are supposed to be before we can help you write axios code to send that. Also, are you sending this from a browser? If so, you may have cross origin issues since custom headers trigger extra cross origin checks such as an OPTIONS request to pre-flight the request which may or may not be supported by your target. – jfriend00 Dec 31 '19 at 04:26
  • @jfriend00 I updated the post. Thanks for responding! It is not from a browser, it's a request coming from a discord bot run using heroku. – Jordy337 Dec 31 '19 at 04:44
  • I wonder if you need the axios option `withCredentials: true`? – jfriend00 Dec 31 '19 at 05:30

3 Answers3

3

axios.get() and axios.put() do not have the same arguments, but you are trying to use the same arguments for both.

axios.get(url[, config])
axios.put(url[, data[, config]])

Even though you don't have any data for your .put(), you have to put something in the second argument position because without it, axios is taking your config object as the data and not using it as the config which is likely why the custom headers are not getting set as desired.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

Try this and let me know if it works.

{ headers: {"Authorization" : `Bearer ${AUTH-KEY-GOES-HERE}`} }

Note: Make sure your server is set up so it responds to an OPTIONS request at that url with an Access-Control-Allow-Headers: Authorization header.

james emanon
  • 11,185
  • 11
  • 56
  • 97
0

Issue is with the way you're passing the headers to the axios.put() method. You have included the headers object as the second argument of the method, but it should be included as the third argument.

Correct Answer :

const response = axios.put(
      url, 
      {},
      {
        headers: {
          Authorization: 
          'Access-Control-Allow-Origin': ,
        },
      }
    );
borchvm
  • 3,533
  • 16
  • 44
  • 45