Google Form Submission
Last Updated: 2025-05-16
A step-by-step guide for capturing Google Form responses and forwarding HTTP POST requests to a C# API endpoint.
1. Prerequisites
- A Google Workspace or personal Google account with access to the target Form in the Google Drive and access to its Apps Script page
- A C# API project with a publicly reachable endpoint (for example, via ngrok or deployed to Azure App Service).
2. Configuring the Google Form
2.1. Create or Open Your Form
- In Google Drive, locate the existing Form or create one if it does not exist.
- To create, click New → More → Google Forms and build your form (add questions, titles, etc.).
2.2. Add an Apps Script
- In the Form editor, click the ⋮ menu at the top right and choose Apps Script.
- In the Apps Script project, click the + icon in the left panel and select Script to create a new script file (e.g.,
Code.gs). - Replace its contents with the following JavaScript, making sure to update the
POST_URLconstant to your C# endpoint URL:
// Replace with your target API endpoint URL
var POST_URL = "https://your-api-endpoint.com/api/form/submit";
function onFormSubmit(e) {
var form = FormApp.getActiveForm();
var allResponses = form.getResponses();
var latestResponse = allResponses[allResponses.length - 1];
var response = latestResponse.getItemResponses();
var payload = {};
for (var i = 0; i < response.length; i++) {
var question = response[i].getItem().getTitle();
var answer = response[i].getResponse();
payload[question] = answer;
}
var options = {
'method': 'post',
'contentType': 'application/json',
"payload": JSON.stringify(payload)
};
try {
var response = UrlFetchApp.fetch(POST_URL, options);
Logger.log(response.getContentText());
} catch (error) {
Logger.log("Error sending data: " + error.toString());
}
}
2.3. Create a Trigger
- In the Apps Script editor, click Triggers (clock icon) in the left panel.
- Click Add Trigger at the bottom right. Configure as follows:
| Field | Value | Notes |
|---|---|---|
| Function to run | onFormSubmit |
Matches the function name in your script. |
| Deployment | Head |
|
| Event source | From form |
Other options: Time-driven, Calendar. |
| Event type | On form submit |
Other From form options: On open |
| Failure notification settings | Notify me immediately |
Adjust to preference. |
- Click Save. Your script will now execute whenever the Form is submitted.
3. Implementing the C# API Endpoint
3.1. Define the Controller Action
In your ASP.NET Core API project, add or update a controller to handle incoming submissions:
[ApiController]
[Route("api/[controller]")]
public class FormController : ControllerBase
{
private readonly IMediator _mediator;
public FormController(IMediator mediator)
{
_mediator = mediator;
}
[HttpPost("submit")]
public async Task<IActionResult> ReceiveForm([FromBody] Dictionary<string, string> formData)
{
// Publish or process the form data as needed
await _mediator.Publish(new FormSubmittedEvent(formData));
return Accepted();
}
}
- Route:
POST https://your-api-endpoint.com/api/form/submit - Payload: A JSON object where each key is the question title (including punctuation) and the value is the user’s response.
3.2. Sample Request Body
Below is an example JSON body your endpoint might receive (note the : at the end of each key):
{
"First name:": "Jane",
"Surname:": "Doe",
"Date of birth:": "1990-01-15",
"Age:": "50",
"House name / number and street:": "1 Example Street",
"Town / city:": "London",
"County:": "Worcestershire",
"Postcode:": "A1 2BC",
"Please provide the reason for submitting:": "Testing submission"
}
4. Conclusion
By following this guide, your Google Form should now automatically send submissions to your C# backend, enabling real‑time processing and integration with your application workflow.
5. Further Actions
- Authentication: Protect your endpoint by requiring an API key or OAuth token. Add a header (e.g.,
x-api-key) inoptions.headerswithin your Apps Script to ensure only authorized requests are accepted. - Use an Outbox Message Pattern: Implement an
outboxMessagetable or queue in your C# backend to record incoming submissions before processing; this ensures reliable delivery, supports retry mechanisms, and provides durability in case of transient failures. - Validation: Implement server‑side validation of the payload to prevent malformed data.
- Retries & Logging: Enhance error handling in Apps Script to retry failed requests or log errors to a persistent storage (e.g., Cloud Logging).
- Security & Rate Limiting: Consider IP whitelisting, rate limits, or CAPTCHA on the Form to mitigate abuse.