Getting an error while trying to fetch individual posts

I’m trying to fetch the data of specific posts in Discourse. But while trying to do that, I’m getting the error that access is restricted, so I proceeded to add my API keys to the header. To do that I’ve written the following code:

axios.get(`{myURL}/posts/${pid}.json`, {
    'mode': 'cors',
    headers: {
      Authorization: '{myAPIKey}'
    }})
    .then(function (response) {
      consol.log(response)
      setPostData(response.data?.topic_list?.topics)
    }) 
    .catch(function (error) {
      console.log(error)
    })

I’ve already added CORS to de Discourse server to allow requests from my server. Issue with this is that now I’m getting a CORS error which says the following: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I don’t understand what may I have done wrong as everything seems okay

1 Like

Here is an authorization header that I use in Ruby:

headers = { ‘Authorization’ => “Bearer #{do_api_key}” }

So I think that you need to add Bearer beforeyour api key.

Here are headers:

         headers = { 'api-key' => discourse_api_key, 'api-username' => 'system' }

So I think you need headers like those. Discourse API Documentation is supposed to help with that.

3 Likes