How can I authenticate by rest api oauth2

It doesn’t seem to be working because we do not see the value in the Request Header’s Authorization.

Formatters were not ok, I defined formatters by this post and it is working-

However now i am getting 500 errors

w2 w3

I think the problem is how to pass grant_type etc in data. I checked with following variations-
grant_type=password
username=admin
password=admin

or body =grant_type=password&username=admin&password=admin

or Form data_type is not available in AC api action as in SC api action (where it is working)
regards

This may be another bug in the AC API Action. @George can you confirm?

Your headers say that you’re sending this as application/x-www-form-urlencoded

But the Payload is formatting it as multipart/form-data, although it looks malformed because you shouldn’t see anything other than the key:value pairs unless you click “view source”.

Correctly formed data for multipart/form-data would look like this.

image
image

Since you’re sending as application/x-www-form-urlencodedyour payload should look like this, but it doesn’t for some reason.

grant_type=password&username:admin&password:admin

I’m curious if the SC API Action is honoring the application/x-www-form-urlencoded header you set or if it’s sending everything as multipart/form-data to your API.

If I give grant_Type, username, password separately then the pay load is as below (This is how i am giving data in SC API action and its working) -

regards

That still doesn’t appear properly formatted. What does it look like in Wappler?

What happens if you use an AC API form and put all of the fields inside of the form?

image

<form is="dmx-api-form" id="apiform1" method="post">
    <input name="grant_type" value="password">
    <input name="username" value="admin">
    <input name="password" value="admin">
</form>

Same as in API Action, same error and same payload

Are you still adding application/x-www-form-urlencoded? If so, can you remove it and see if that makes a difference?

1 Like

This is the weird part that I think is not being formatted properly by Wappler.

I think the “grant_type” should appear after name like this.
Content-Disposition: form-data; name="grant_type"

1 Like

Thanks @kfawcett, after removing content-type both api action and api form are working.

So Wappler AC APIs do not work properly? They ignore the content-type being set and try to send everything as form-data. :exploding_head:

It depends on the Data Type that is set, Auto will convert the data to FormData, JSON will become a JSON object and Text will send use raw text. It will set the corresponding Content Type header which is needed.

If you overwrite the Content Type header you should use the Text data type and then set the body you want to send in the Text Data.

So if you want urlencoded you add the Content-Type header and set it to application/x-www-form-urlencoded. Set Data Type to Text and set Text Data to grant_type=password&username=admin&password=admin.

1 Like

It appears that Text Data is not dynamic.
image

Seems we indeed don’t have a way to add dynamic text data. You can do it in codeview by prefixing the data attribute with dmx-bind:.

dmx-bind:data="grant_type=password&username={{username}}&password={{password}}"
1 Like