breezy
breezy15mo ago

API POST to PHP file doesn't do anything

I have created an agent that uses an API to POST data to a PHP file as form data. The PHP file is supposed to take the form data that are passed to it from Voiceflow and create a new row in a sql database. I have tested the PHP file at length, it works great if I pass data to it from an HTML form. However, when I run the agent, and have Voiceflow POST the data to the PHP file nothing happens. The chat interface runs smoothly, no errors, looks like everything worked, but nothing is written to the database. The headers are "Content-Type" and "application/x-www-form-urlencoded", the body is set to "Form Data"", and I am passing one variable. When I test the API in Voiceflow I get: Body { "VF_STATUS_CODE": 200, "VF_HEADERS": { "connection": "Upgrade, close", "content-encoding": "gzip", "content-length": "2894", "content-type": "text/html; charset=UTF-8", "date": "Fri, 21 Jun 2024 23:35:08 GMT", "server": "Apache", "upgrade": "h2,h2c", "vary": "Accept-Encoding", "x-powered-by": "PHP/7.2.34" } } Headers content-type application/json; charset=utf-8 I have worked on this for two days and cannot seem to overcome this problem. The test shows Header as "application/json; charset=utf-8", but in the API Header I entered is "application/x-www-form-urlencoded". Does the Voiceflow API POST JSON objects or form data when I set "Content-Type" to "application/x-www-form-urlencoded"? I am a novice and may be missing something obvious to other users. Thank you in advance for any advice you can provide.
3 Replies
Tico | Voiceflow Assistant
The Voiceflow Fetch API is a modified version of the standard Fetch API. It supports standard fetch options such as method, headers, and body. For example, to perform a POST request, you can use the following TypeScript code:
await fetch('YOUR-NGROK-URL-HERE', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({name: 'name', age: 'age'})
});
await fetch('YOUR-NGROK-URL-HERE', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({name: 'name', age: 'age'})
});
The first argument of fetch is the URL of the server. The second argument is an options object. In this case, the method is 'POST', the Content-Type header is set to 'application/json', and the body is a JSON stringified object. Voiceflow also supports various trace types, including text, visual content, cards, and more. These traces are response segments from an interaction with the assistant. For more detailed information on using the Voiceflow Fetch API, you can refer to the Voiceflow Functions Documentation.
breezy
breezyOP15mo ago
This response does not help.
W. Williams (SFT)
Your response must contain valid json.

Did you find this page helpful?