Forge webtrigger - read body of request

Hi,

I would like to post a json body to a webtrigger. I couldn’t find any peace of information on how to do this. I’ve also checked via debugging the standard hello world example if anything is passed to “runSync”/“runAsync”.

Question: I can I access the request from within “runSync”/“runAsync”? #codegeist

Thanks.

Hi @SvenLauterbach ,

Your should be able to POST JSON to a webtrigger like you would to any REST API endpoint that accepts the POST method.

If you’re posting from a NodeJS runtime, then perhaps use node-fetch as follows:

fetch(your-webtrigger-url, {
  method: 'post',
   body:    JSON.stringify(your-object-to-post),
   headers: { 'Content-Type': 'application/json' },
})
.then(etc);

Can you share some code to help us debug your issue.

Regards,
Dugald

Hi Dugald,

sorry for phrasing the question ambiguous. Its not about sending a post request, but accessing the information from the request in the webtrigger. The synchronous trigger generated by “forge create” is the following snippet:

exports.runSync = () => {
  const result = buildOutput(Math.random());
  return result;
};

The question is, how can I access the json from the body your snippet posted to the webtrigger? Like is there a global object i can use?

exports.runSync = () => {
 const result = buildOutput(request.body.propertyOfInterest);
 return result;
};

Or is there a parametet which is passed to the “unSync” method?

exports.runSync = (request) => {
 const result = buildOutput(request.body.propertyOfInterest);
 return result;
};

So how can I access the information from the http request (which triggers the webtrigger) in my runSync() function?

1 Like

Hi @SvenLauterbach ,

Yes, a request parameter is passed to the runSync method. The following should work:

exports.runAsync = (request) => {
  const postBody = JSON.parse(request.body);
  const propertyOfInterest = postBody. propertyOfInterest;
}

Regards,
Dugald

4 Likes

HI @SvenLauterbach
The following typescript interface definition should help you out.

interface WebTriggerRequest {
  body: string;
  headers: {[headerName: string]: string[]};
  method: string;
  path: string;
  queryParameters: {[paramName: string]: string[]};
}

The request is documented here on the web-trigger/request page

4 Likes

@Roaan curious where this type definition is from and if you know a way to access it in app code (via @forge/api or similar)?

If not, it would be good if the WebTriggerRequest type could be exposed in the Forge API package so apps can type web trigger functions accordingly.