Jump to content
Brent_E

RESOLVED: 404 Error When Attempting To Create A Session

Recommended Posts

Good evening,

I thought I had my session code squared away, but appears I was wrong. I am new to the Forex API and I am experiencing a strange error when I try to create my first session.

ISSUE:

I am utilizing the 'requests' library in python and Google Colab to interact with the API. I am attempting to create a session per the URI in the 'LogOn' documentation combined with a payload containing the username, password and appkey provided to me when my API account was set up. I've confirmed and re-confirmed several times that I am using the correct URI, username, password, and appkey.

(For this post, I've changed the payload values to generic values.)

 I've also included a few print statements to provide some visibility of the system responses.

Upon running my code (please see below), the status code returns a value of 404.

In: 

import requests
 
payload = {"Password":"openthedoor",
                    "AppVersion":"1",
                    "AppComments":"",
                    "UserName":"DM123456",
                    "AppKey":"ABC"
                    }
 
response = requests.post('https://ciapi.cityindex.com/TradingAPI/session/post', data=payload)
 
print(response.url)           
print(response.status_code)   
print(response.text)  

Out:

      https://ciapi.cityindex.com/TradingAPI/session/post

      404

 

Does anybody know why this is occurring and what I might be able to do to resolve it?

Thanks in advance,

....

 

 

 

 

 

 

Share this post


Link to post

Hi Brent,

There is an extra "/post" in your URL that should not be there. The correct URL is: https://ciapi.cityindex.com/TradingApi/session/

The screenshot shows that the URL used is https://ciapi.cityindex.com/TradingApi/session/post

Please remove the extra part at the end of the URL and it should hopefully work then.

Kind Regards, PM

Share this post


Link to post

@Physicasman

Good day, PM

Thanks very much for your respose and assistance. 

My apologies, I should have mentioned the following in my original post, but I didn't want to confuse the issue.

I did try the URL without the "/post" at the end, and I received a different error. (I will post the code and result below for details.)

I am curious if maybe I need to identify something more regarding 'content-type' in the header or maybe something more in json.

 

NOTE I don't know if this is significant or not, but I am trying to log onto a demo account.

Also, my account is a commission based account.

 

This is my code and error when I try without the "/post" at the end:

import requests
 
payload = {"Password":"openthedoor",
           "AppVersion":"1",
           "AppComments":"",
           "UserName":"DM123456",
           "AppKey":"ABC"
}
 
response = requests.post('https://ciapi.cityindex.com/TradingApi/session/', data=payload)
 
print(response.url)           
print(response.status_code)   
print(response.text)    

 

Result: with error message ending with "ErrorCode":4005

https://ciapi.cityindex.com/TradingApi/session/

400

{"HttpStatus":400,"ErrorMessage":"The request content-type is not supported","ErrorCode":4005}

 

I did find a prior post regarding a similar issue for a person using java....But I am not sure if this would solve my issue or how to implement this in python...

=============================================================================

Also, just out of curiousity, I found another potential solution online. I tried including more info in the header in hopes to trigger a content-type or other json response, and I got an even stranger error message yet....

This is the code of that example and the result:

import requests
import json
 
Newheader = {'Content-type': 'application/json', 'Accept': 'text/plain'}
 
payload = {"Password":"openthedoor",
           "AppVersion":"1",
           "AppComments":"",
           "UserName":"DM123456",
           "AppKey":"ABC"
}
 
response = requests.post('https://ciapi.cityindex.com/TradingAPI/session/', data=payload, headers=Newheader)
 
print(response.url)           
print(response.status_code)   
print(response.text)          

Result:  with error message ending with "ErrorCode":4002

https://ciapi.cityindex.com/TradingApi/session//

400

{"HttpStatus":400,"ErrorMessage":"The request body cannot be null.","ErrorCode":4002}

=============================================================================

 

What else could we try to resolve this issue? I am more concerned about the first error than the second.

Please let me know your thoughts.

Thanks Much,

 

Share this post


Link to post

Hi Brent,

Thank you for the detailed investigation and report on results - it is very helpful. 

From what we're seeing, the current working theory is that the 4005 error code is due to the same reason you found on the different forum post. The header application type needed to be specified explicitly as 'application/json'. 

When you included the application type in the header, the 4002 error seems to indicate that the data payload is being sent in the header rather than in the body. Hence, why it is complaining that the request body cannot be null. 

In the other forum thread you mentioned using the Postman API tool to help with your development. It would be a good check to see if you can login to your account using Postman. This can confirm whether the problem is in the Python code or elsewhere.

If you are not up to speed with Postman yet, since you are using a demo account and not a real money account you can send me your demo account credentials via a private message on this forum. I can then try logging in with that account. If login is successful, it will indicate that the problem is in the Python code. Unfortunately, as we are not Python developers we can't help debug the Python code.

Kind Regards, PM 

Share this post


Link to post

Good afternoon, PM.

Ok, that sounds like a plan. I have sent my demo account credentials to you via private message. Please keep an eye out for that.

I look forward to seeing what you discover.

Thanks again,

Brent

Share this post


Link to post

@Physicsman and Forum

Good day everybody,

Together with Physicsman we were able to resolve the above issue.

Thanks again, Physicsman, for your assistance and determination in finding a solution.

For those of you who are attempting to log onto the API using python, please review the following code snippet and result.

NOTE language: python,      platform:  Google Colab 

RECOMMENDATIONS:

1.) Call customer service to confirm two things:

          1.) there are NO security locks for multiple failed attempts on the account you are trying to log onto

          2.) confirm you account details (username, password, and AppKey). I discovered that the accounts team accidentally sent me the account number as the username.

              But they were able to quickly correct this and provide me the correct username. So, make sure to confirm these details with customer service.

 

Once you have done these things, try using the code below:

Remember to substitute your credials for your specific account.

Code

import requests
import json
 
url = 'https://ciapi.cityindex.com/TradingAPI/session/'
 
data = {'Password':'XXXXXXX',
        'UserName':'DM123456',
        'AppKey':'ABC'
}
 
response = requests.post(url, json=data)
 
print(response.url)           
print(response.status_code)   
print(response.text)          

 

Result:

Response 200 means successful connection.

From the response you can get your session id for other API functions

https://ciapi.cityindex.com/TradingAPI/session/

200 {"Session":"081ei2-awe234322-we23dr-223serrwd","PasswordChangeRequired":false,"AllowedAccountOperator":false,"StatusCode":1,"AdditionalInfo":null,"Is2FAEnabled":false,"TwoFAToken":null,"Additional2FAMethods":null,"UserType":1}

 

 

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×