OPTIONS instead of POST

Hi,

I am using the API Form to send a POST request, but instead it creates an OPTIONS request.

This is the link you can use to see that behaviour: https://www.foxy88.net/test.php

This is the screenshot that shows the request:

Can you help me, please?

Thanks.

That is because you do a CORS request, your site and the api are on different domains. It sends an OPTIONS request first to check if it may connect using CORS. After it is allowed it will send the actual POST request.

Noticed that the target in your test page doesn’t allow CORS requests. So the request is blocked by the API endpoint.

Access to XMLHttpRequest at ‘…’ from origin ‘https://www.foxy88.net’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

1 Like

Thanks @patrick for your reply.

Are you saying that the Webhook.site (https://webhook.site) does not allow CORS requests?

It is very strange because it should allow any kind of requests.

1 Like

I found this https://github.com/fredsted/webhook.site/issues/26

So, there is a problem with that website.

Let me intercept the request locally so I can check if it is POST or OPTIONS.

Thanks @George, I can see it now.

I found this https://github.com/fredsted/webhook.site/issues/26
So, there is a problem with that website.

Let me intercept the request locally so I can check if it is POST or OPTIONS.

I have found this https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

The issue is generated automatically by the browsers.

Do you know if there is a way to avoid that?
I mean… I guess that all the APIs calls to external (different) domains are not possible then…

There is no workaround in the browser, the only workaround is by using the Server-Side API or to have an API with CORS support that serves as a proxy.

https://bypasscors.herokuapp.com/

1 Like

For Server-Side API, do you mean using the Server Connect Form in Wappler?

No - We have the same API Connector as action in Server Connect - to post and retrieve data to remote API’s

Unfortunately it doesn’t support file uploading there. Just regular data posting and retrieval.

1 Like

I do not need to upload anything. I just need to send a simple POST request.

Is it possible?

Yes of course - that is it all for

Check the API Action - in Server Connect.
It has similar parameters as the client side - bu then the API requests are executed server side, so you don’t have any CORS troubles.

1 Like

Now I have to understand how to create a normal form and using API Action + Server Connect then… :scream::scream::scream:

I am trying to use the API Action in the Execute Action Steps using Server Connect, but I get the following error:

{"code":0,"file":"C:\xampp3\htdocs\test\dmxConnectLib\modules\api.php","line":187,"message":"count(): Parameter must be an array or an object that implements Countable","trace":"#0 [internal function]: exception_error_handler(2, 'count(): Parame...', 'C:\\xampp3\\htdoc...', 187, Array)\n#1 C:\xampp3\htdocs\test\dmxConnectLib\modules\api.php(187): count(Object(stdClass))\n#2 C:\xampp3\htdocs\test\dmxConnectLib\lib\App.php(159): modules\api->send(Object(stdClass), 'api1')\n#3 C:\xampp3\htdocs\test\dmxConnectLib\lib\App.php(98): lib\App->execSteps(Object(stdClass))\n#4 C:\xampp3\htdocs\test\dmxConnectLib\lib\App.php(71): lib\App->exec(Object(stdClass))\n#5 C:\xampp3\htdocs\test\dmxConnect\api\Test\api2.php(8): lib\App->define(Object(stdClass))\n#6 {main}"}

I have tried to change the type of the form field in the Globals from Text to Array and to Object without success.

Why am I getting 500 Internal Server Error?

Thanks.

1 Like

It works now! :smiley:

I get the POST request without any CORS issues.

Thanks for your support!

2 Likes

Good to hear! What was causing the problem? It’s always good to post solutions and guides here on the forum so others can learn as well

1 Like

The issue is generated automatically by the browsers.

Using the API Action in Server Connect, the API requests are executed server side, so there are not any CORS issues.