Jump to content
  • 0

API: How to delete a position, Java


KML

Question

Dear all,

I'm having trouble deleting a position using the API. As I understand https://labs.ig.com/rest-trading-api-reference/service-detail?id=678, I'm supposed to send a DELETE request with a body. But isn't a DELETE-request supposed to be without a body?

The line

     HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
           .headers("Content-Type", "application/json; charset=UTF-8", "Accept", "application/json; charset=UTF-8",
            "Version", "2", "IG-ACCOUNT-ID", CAI,
            "X-IG-API-KEY", APIKEY, "CST", CST)
            .method("DELETE", HttpRequest.BodyPublishers.ofString(body))  
            .build();

returns a 404 not found error, but if I change DELETE to POST as in 

     HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
           .headers("Content-Type", "application/json; charset=UTF-8", "Accept", "application/json; charset=UTF-8",
            "Version", "2", "IG-ACCOUNT-ID", CAI,
            "X-IG-API-KEY", APIKEY, "CST", CST)
            .method("POST", HttpRequest.BodyPublishers.ofString(body))  
            .build();

I get an expected error {"errorCode":"invalid.request.format.line-1.column-3"}. Does this mean that I'm supposed to use the POST method anyway? How does it then know that I',m deleting the position and not creating a new one?

I could of course delete a long position by creating a short position and vice versa, but that looks a bit messier.

Thanks for any help or clarifications.

Link to comment

6 answers to this question

Recommended Posts

  • 0
1 hour ago, KML said:

Dear all,

I'm having trouble deleting a position using the API. As I understand https://labs.ig.com/rest-trading-api-reference/service-detail?id=678, I'm supposed to send a DELETE request with a body. But isn't a DELETE-request supposed to be without a body?

The line

     HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
           .headers("Content-Type", "application/json; charset=UTF-8", "Accept", "application/json; charset=UTF-8",
            "Version", "2", "IG-ACCOUNT-ID", CAI,
            "X-IG-API-KEY", APIKEY, "CST", CST)
            .method("DELETE", HttpRequest.BodyPublishers.ofString(body))  
            .build();

returns a 404 not found error, but if I change DELETE to POST as in 

     HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
           .headers("Content-Type", "application/json; charset=UTF-8", "Accept", "application/json; charset=UTF-8",
            "Version", "2", "IG-ACCOUNT-ID", CAI,
            "X-IG-API-KEY", APIKEY, "CST", CST)
            .method("POST", HttpRequest.BodyPublishers.ofString(body))  
            .build();

I get an expected error {"errorCode":"invalid.request.format.line-1.column-3"}. Does this mean that I'm supposed to use the POST method anyway? How does it then know that I',m deleting the position and not creating a new one?

I could of course delete a long position by creating a short position and vice versa, but that looks a bit messier.

Thanks for any help or clarifications.

Dear @KML,

Thank you for your post, please note that many members on our community can assist with complex API-related queries. You can check out posts and replies by @andysinclair and @bug-or-feature on similar topics.

Thanks,

KoketsoIG

Please rate us on Trustpilot: IG Trustpilot 

Link to comment
  • 0
27 minutes ago, bug-or-feature said:

The way you do it is to create a POST, and a header named "_method" with value "DELETE"

I was just about to reply with this.

I have it working with the following in the body: dealId, size, direction and orderType

  • Like 1
  • Thanks 1
Link to comment
  • 0

Thanks for all your replies! Highly appreciated! 🙂

However, I'm still not able to cross the finish line. If I proved the following headers:
{_method=[DELETE], Accept=[application/json; charset=UTF-8], Content-Type=[application/json; charset=UTF-8], CST=[removed], IG-ACCOUNT-ID=[removed], Version=[2], X-IG-API-KEY=[removed]}

I get the 404 File not found error.

However, when I use the following headers:
{Accept=[application/json; charset=UTF-8], Content-Type=[application/json; charset=UTF-8], CST=[removed], IG-ACCOUNT-ID=[removed], Version=[2], X-IG-API-KEY=[removed]}

I get {"errorCode":"validation.null-not-allowed.request.expiry"} which is expected as I don't provide sufficient information to create a new position. (I guess that is what IG thinks I'm trying to do when I don't add the _method= DELETE?

Any ide what I'm missing/misunderstanding?

I still have the option to delete a position by creating a new with a opposite buy/sell attribute, but it would feel more satisfying making this work. 🙂

Link to comment

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
  • image.png

  • Posts

    • Telcoin's definitely shaking things up with affordable mobile transfers! But MOB sounds interesting for secure everyday payments. Abra seems like a one-stop shop for global money stuff. Telcoin being on Bitget is cool, but competition is healthy! Maybe Telcoin can focus on low-cost remittances while MobileCoin tackles daily purchases. Telcoin's not the only player, but they're definitely a piece of the puzzle.
    • Telcoin is a frontrunner in leveraging blockchain technology for affordable and accessible mobile money transfers. Intrigued by this concept? Let's explore other projects working to revolutionize how we send and receive money around the world, with a focus on mobile solutions. MobileCoin (MOB), this project aims to provide a secure and private mobile-first cryptocurrency specifically designed for everyday transactions. Abra (ABRA), this global investment app allows users to buy, sell, and trade various cryptocurrencies and assets, including sending and receiving money internationally. So, is Telcoin the only game in town for mobile money transfers cause they got listed on Bitget? Maybe or maybe not! There are established players and innovative startups tackling this challenge. What do you think? Is Telcoin the future of mobile money transfers, or just a piece of the puzzle? Have you explored any other mobile money transfer projects? Share your thoughts in the comments!
    • Mande Network's vision of an "Onchain Credibility Hub" for Web3, powered by "Proof-of-Credibility" and a "Human Capital Network," is intriguing. Decentralized Society (DESO), this project aims to create a social network where users own their data and can monetize their content and contributions. Human Protocol (HMN), this platform connects skilled individuals with tasks and projects, focusing on building a more efficient and transparent talent marketplace. Is Mande Network the ultimate solution for valuing human capital in Web3 cause they are listed on Bitget? Maybe or maybe! It's a promising project, but there are others addressing this space. What do you think? Is Mande Network a revolutionary force in human capital valuation, or just one step in the evolution of Web3? Have you explored any other projects focused on human capital or reputation in the decentralized space? Share your thoughts in the comments!
×
×
  • Create New...
us