No logs for failed POST

Having just gone through the Forge tutorial, I’m trying to play around and do some more advanced things.

One thing I’m trying to do is create pages in the same Confluence space at the click of a button. However, my request never completes, at least as far as I can tell.

const createPage = async (space) => {
    console.log("Okay, let's try and create a page in ", space)
    const data = await api
        .asUser()
        .requestConfluence(`/rest/api/content`, {
            method: 'POST',
            body: JSON.stringify({
                title: 'Thing',
                type: 'page',
                space: space,
                body: {
                    storage: "New page stuff",
                    representation: "view"
                }
            })
        }).then(stuff => {
            console.log("Stuff", stuff)
            return stuff
        }, (rejectionReason) => {
            console.error("Nah, yer rejected", rejectionReason)
        }).catch(e => {
            console.error("Received error", e)
        })

    console.log("Got some data back:", data)
    return data
}

I’ve run forge lint --fix, and my manifiest does indeed have the required scopes in my manifest.

permissions:
  scopes:
    - write:confluence-content
    - read:confluence-content.summary

I can’t find anything in forge logs to indicate what’s happening with my request. Possibly I’m just a bit newbish with node-fetch, but I don’t get why there’s nothing in the logs.

Hey @jcarter,

I assume you have a button that is doing something like this?

<Button onClick={() => createPage('my-space')}>
  click me!
</Button>

Assuming the function is being triggered ok, I have added some additional logs to the response which may provide some more insight into if the request is happening.

const createPage = async (space) => {
  console.log("Okay, let's try and create a page in ", space)

  return await api
    .asUser()
    .requestConfluence(`/rest/api/content`, {
      method: 'POST',
      body: JSON.stringify({
          title: 'Thing',
          type: 'page',
          space: space,
          body: {
              storage: "New page stuff",
              representation: "view"
          }
      })
    }).then(async response => {
      console.log("Status Code", await response.status)
      console.log("Body", await response.text())
      
      return response;
    }).catch(e => {
      console.log("Received error", e)
    })
}

Let me know if this tells you anything more :slight_smile:

@danielwinterw - thanks for the reply, but unfortunately, it doesn’t. Here’s my whole index.tsx file for my app:

import api from "@forge/api"
import ForgeUI, {Button, Fragment, Macro, render, useProductContext} from '@forge/ui'

const createPage = async (space) => {
    console.log("Okay, let's try and create a page in ", space)
    const res = await api
        .asUser()
        .requestConfluence(`/rest/api/content`, {
            method: 'POST',
            body: JSON.stringify({
                title: 'Thing',
                type: 'page',
                space: space,
                body: {
                    storage: "New page stuff",
                    representation: "view"
                }
            })
        }).then(async response => {
            console.log("Status Code", response.status)
            console.log("Body", await response.text())
            return response
        }).catch(e => {
            console.error("YOU BWOKE IT")
            console.error(e)
        })
}

const App = () => {
    const context = useProductContext()

    return (
        <Fragment>
            <Button
                onClick={() => {
                    console.log("I push da buddin!")
                    createPage(context.spaceKey)
                        .then((data) => console.log(data))
                }}
                text={"Make that page"}
            />
        </Fragment>
    )
}

export const run = render(
    <Macro
        app={<App/>}
    />
)

And here’s what I get in the logs of forge tunnel:

invocation: cdbcd60df3e9be0b index.run
INFO    14:33:39.459  cdbcd60df3e9be0b  I push da buddin!
INFO    14:33:39.466  cdbcd60df3e9be0b  Okay, let's try and create a page in  TEST 

It’s clear that the createPage function gets called, but it looks like my then functions don’t ever execute, whether they’re async or not.

Okay, after some careful tutelage from my betters on the finer points of async/await, I have it sorted.

import api from "@forge/api"
import ForgeUI, {Button, Fragment, Macro, render, useProductContext} from '@forge/ui'

const createPage = async (space) => {
    console.log("Okay, let's try and create a page in ", space)
    const resp = await api
        .asUser()
        .requestConfluence(`/rest/api/content`, {
            method: 'POST',
            body: JSON.stringify({
                title: 'Thing',
                type: 'page',
                space: space,
                body: {
                    storage: "New page stuff",
                    representation: "view"
                }
            })
        });
    console.log("Status Code", resp.status)
    const text = await resp.text()
    console.log("Body", text)
    return text
}
const App = () => {
    const context = useProductContext()
    return (
        <Fragment>
            <Button
                onClick={async () => {
                    console.log("I push da buddin!")
                    const page = await createPage(context.spaceKey)
                    console.log(page)
                }}
                text={"Make that page"}
            />
        </Fragment>
    )
}
export const run = render(
    <Macro
        app={<App/>}
    />
)
2 Likes