REST API/HTTP Request 401 Unauthorized Error

1
Hi all,   I am encountering some issues sending an HTTP Request to an external system with an exposed REST API. I am creating a POST request where I am sending the username/password credentials to get back an authorization token.    First, I input the JSON structure of the request:   Next, I create a mapping from the Mendix object to the JSON Structure which will be generated automatically by Mendix:   Next in Mendix, I create a Microflow which will call the REST (POST) service to get the necessary authentication token:   Note that I have also tried using HTTP authentication, and it still throws a 401 error. The actual Request which is the JSON Body we were generating earlier is called here:   So, we pass this newHRMSLoginRoot entity/object we created into Mendix and it auto-generates the JSON body for the request.  EDIT: I have added the proper credentials here, but have set them to username and password for security reasons. However, I am getting a 401 error when this request is submitted:   I am stuck here, because I have confirmed the username and password credentials are correct. I have also successfully made this login request from Postman, using the identical credentials: I do not have tons of experience with HTTP requests, so I am turning to you all for help. Please let me know if you need any more information. Thank you for your time. Best, Nick
asked
3 answers
2

Hi NIck,
I assume that the username and password for your API is same everytime, so you have to
enter the username and password in the HTTP authentication section(username and password boxes) in (''),

 

 

this way you'll get the accesstoken,

and for getting the accesstoken, I recommend you using the GET call.

Hope it helps!

 

 

**Edited**

Only the username and password should be there, no headers are required.

answered
1

Hi All,

 

Thank you so much for all your help. After discussing with Rishabh I realized this login only took a JSON body with a username and password as a request, which helped me narrow down the problem further.

 

Then, after tracing through the responses using the REST Consume (thank you Jamie), I realized the problem lied in where I was inputting the username and password. 

 

 

The value, unlike the JSON request, does not need quotations (“ “) around the credentials. After changing this, I was able to successfully authenticate and retrieve my token.

 

- Nick 

 

answered
0

The JSON Structure in the first picture is literally that: a structure that indicates how the JSON object will look like. The actual value will not be copied over, so you should remove the username and password from that. It should look like:

{
    "username": "",
    "password": ""
}

You should put the actual values for the username and password during the Create Object action, which you are now setting to “username” and “password”.

answered