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

    • The digital landscape is undergoing a profound transformation as attention, once a freely given commodity, is increasingly recognized as a valuable asset. Layer3 is at the forefront of this revolution, pioneering a new economy where attention can be owned, traded, and monetized   This innovative approach empowers individuals to monetize their engagement, providing unprecedented control over personal data. Simultaneously, advertisers benefit from transparent metrics that optimize campaign performance. Content creators are presented with diverse revenue avenues beyond traditional advertising, while the overall ecosystem experiences a more equitable distribution of value.   The implications of Layer3 extend across various sectors. Social media platforms, for instance, can leverage this technology to revolutionize user engagement and monetization strategies. Tokenomics play a crucial role in driving Layer3's economy, incentivizing participation and rewarding value creation. While challenges such as data privacy and market volatility exist, the potential benefits of Layer3 are immense   Anticipation is building as its native token $L3 is on Bitget Pre-market as users await its listing on the exchange. This milestone is expected to significantly increase the token's visibility and accessibility, potentially driving substantial growth and attracting new investors. As the countdown begins, the crypto community watches with keen interest, eager to see how Layer3 will perform in this new chapter.
    • I've been exploring the world of play-to-earn gaming recently, looking for something that's not just about endless grinding but actually offers a fun and rewarding experience. OGC really stood out to me because it combines gaming with a sense of community in a unique way. OGC isn't just a game; it's a platform where you can play, earn, and even help shape its future. You're not just a player; you're part of a community with a voice. The idea of earning crypto while playing games is exciting, but what makes OGC special is its focus on community involvement. Your feedback can directly influence the development of the game, which is a big deal. I've also heard that the OGC token is available for pre-market trading on Bitget. While I'm still getting to know the platform and its features, it's definitely something to keep an eye on. Has anyone else tried OGC? What has your experience been like? I'd love to hear your thoughts and any tips you might have.
    • I am anticipating the impact of this listing on Mongy. 
×
×
  • Create New...
us