HTTP Request Data Formatting

Describe the issue/error/question

I am attempting to to authenticate with an API but I’m not sure how to send data for the credentials like I see in the example below where we in curl you’d usually pass it as a -d parameter. I would appreciate any points on how to format such requests.

What is the error message (if any)?

I’m currently running into a 401 issue due to a formatting issue it appears.

API Example curl integration

curl -X POST https://[hostname]:[port]/login.html -d "username=<username>&password=<my password>" --header "Content-Type:application/x-www-form-urlencoded" -c cookie.txt

Information on your n8n setup

  • n8n version: 1.3.0
  • Database you’re using (default: SQLite):
  • Running n8n with the execution process [own(default), main]:
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: desktop app

Hi @cr-rschapman welcome to the community :tada:

Have you tried the http node with Basic Authentication and Credentials?

Screenshot 2022-03-29 at 09.14.31

Example of the http node:

This is without the credentials, which you have to create yourself.

Thank you for the warm welcome!

I did try something similar to that initially but had missed the header set but even after trying to add it now I’m getting a 401. I’ve verified credentials work. The platform allows you to initiate a login and use the session cookies sent back to continue to interact for a short time. It sends the data via curl’s -d data function too. I’m curious how I replicate the curl -d functionality.

curl -X POST https://[hostname]:[port]/login.html -d @login.txt --header "ContentType:application/x-www-form-urlencoded" -c cookie.txt

@cr-rschapman You’re very welcome! Unfortunately, I do not know how to handle session cookies and hope somebody else can answer this question.

Hey all, the cookies are usually exchanged through the headers. @pemontto shared an example showing this for n8n’s own UI recently:

Relevant here is the full response option of the first HTTP Request node (to see the cookie header coming back after logging in):
image

And the cookie header configured on the second HTTP Request node.
Have a look at the above thread to see if this can also apply to you and give me shout if you run into any trouble here.

1 Like

This is very helpful. Thank you!

The question I think I still have is how do I send the data the initial time. I can reuse the cookie but I still need to complete the initial auth.

If I were to do it in python it would look like this:

import requests

username = "<your user name>"
password = "<password>"
server = "<server URL>"
port = "443"

data = {
    "username": username,
    "password": password
}
headers = {"Content-Type": "application/x-www-form-urlencoded"}

base_url = "https://" + server + ":" + port
login_url = base_url + "/login.html"

session = requests.session()
response = session.post(login_url, data=data, headers=headers, verify=True)

print response.status_code
print session.cookies.items()

So it looks like you are sending a POST request with a urlencoded username and password. I think this should do the trick: