URLConnection

Good morning all,

Back on Xojo after a long absence!

My environment is up to date:
Xojo 2019 r3.2
MBS 20.1
MacOS Mojave 10.14.6

I need to connect to a website that provides an API.
This is what the documentation indicates:

  1. CONNECTION
    POST method
    Access url:
    https://opendata-rncs.inpi.fr/services/diffusion/login

Entries:
Variables to be entered in the HEADER Characteristics

login User account login (as transmitted when creating your account)

password User account password (custom)

Exits :
Sample response:
{
username logged in
}

So I tried with the object: URLConnection in a window :

Dim TheUrl as String = “https://opendata-rncs.inpi.fr/services/diffusion/login”

TheWebConnection.ClearRequestHeaders

TheWebConnection.RequestHeader(“login:”) = UserName
TheWebConnection.RequestHeader(“password:”) = Pass

TheWebConnection.Send(“POST”, TheUrl )

with corect login and password as I can log to the site with my browser.

I got an422 error :

{
“globalErrors”: [
“login and passwrod don(t match.”
]
}

So I tried with SetRequestContent :

Dim postData as string = “login: “+ “=” + UserName +”&”+ "password: " + “=”+Pass

TheWebConnection.SetRequestContent(postData, “application/x-www-form-urlencoded”)
TheWebConnection.Send(“POST”, TheUrl )

with a 500 error :
“globalErrors”: [
“RestParam not found : class java.lang.String :Header:login”
]
}

I do not understand what is wrong!

If you had an idea it would be greatly appreciated.

Thanks,
Fred

Did you made sure the Encoding of the Name and Password is correct, before setting them at:

are you sure its written with : ?
RequestHeader(“login:”) <—

Yes, I can connect to the Site with my browser.

I tried both…

Here is a Curl example given in the documentation :

curl -X POST "https://opendata-rncs.inpi.fr/services/diffusion/login" -H  "accept: text/plain" -H  "login: user" -H  "password: pass" -d ""

You can see there is a space after login. But when I try the Curl example on my mac, it fails…

What encoding should be used ?

that is input for a command line tool but http request header is different.
it could also be key: value as form data. ahh you already tested it. “login: “+ “=” looks odd.
with firefox you could see the network data 1:1

In most cases UTF8. Maybe break in the debugger and take a look at the encoding of both strings. If they have no encoding defined, try to do something like Pass.DefineEncoding( Encodings.UTF8 )

can you test
Dim postData as string = “login=” + UserName + ”&password=” + Pass

with Content-Type: application/x-www-form-urlencoded

Hi Markus,
I tested :

'Dim postData as string = “login=”+ UserName2 + “&password=” + Pass2
'Dim postData as string = “login:”+ UserName2 + “&password:” + Pass2
Dim postData as string = "login: "+ UserName2 + "&password: " + Pass2

Always get an 500 error…

@SaschaS : I checked encoding, UTF8 is used…

i think here you was near by. have you looked the data at a break point?
its maybe given as base64?
“login and passwrod don(t match.”

how firefox can display the requests & response.

Then you could try it with http://documentation.xojo.com/api/text/encoding_text/encodebase64.html

TheWebConnection.RequestHeader(“login") = UserName
TheWebConnection.RequestHeader(“password") = Pass

Your “:” is already created by xojo

BTW: If you are on macOS, i can highly recommend https://paw.cloud/ with the Xojo Extensions (Xojo Paw Extensions). :slight_smile:

1 Like

Hi, we progress !

Using
TheWebConnection.RequestHeader("login ") = UserName
TheWebConnection.RequestHeader("password ") = Pass

Without “:” but with a space, I get this :

400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.

Here is what I find in Firefox

referer=https%3A%2F%2Fdata.inpi.fr%2F&login_form%5BEmail%5D=MYUSERNAME&login_form%5Bpassword%5D=MYPASSWORD&login_form%5Blicence%5D=1&login_form%5Bsubmit%5D=&login_form%5B_token%5D=hdww7qJwnIPk3qMqY_VNg9yT9Z6stzRrWvIQp4NOwXA

Use .ClearRequestHeaders() to clear the old headers if you reuse the socket.

Try this after it:
Then set the
.RequestHeader(“login”) = EncodeURLComponent(login)
and
.RequestHeader("password ") = EncodeURLComponent(pass)

Should not be required but you’ll never know i don’t know how their request is handled.

Are you sure they require this content type: “application/x-www-form-urlencoded”
otherwise try: “multipart/form-data”

Not Sure, I understand you correctly, DerkJ. I thought we could request only one Header.

How do you code that, please ?

had you also test with sending this accept: text/plain in header?
because it is part of this curl from documentation.

No, I don’t know how to do that, and the curl example doesn’t work here… I don’t know why…