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 $CATS airdrop has officially started today, which is great news! Bitget is distributing 30k $CATS to the first 200k users, along with other exciting rewards. To qualify, follow these steps: 1. Open the bot. 2. Select Bitget as the exchange to deposit (it’s listed first). 3. Submit your UID and Cats deposit address. 4. Click send. It's that easy! If you've been farming $CATS, this is a great opportunity to boost your holdings with just a few simple tasks.  
    • The changes around sports betting and online gambling have really opened things up. It’s crazy how much the market is growing, and it’s not just for hardcore gamblers—there’s room for casual players too.
    • Cats and Dogs remain the most influential crypto meme community with every blockchain having its own. Recently, Ton blockchain witness the successful listing of its first Dogs meme which did a massive airdrop to users to the point the airdrop is now the yardstick for measuring successful project on Ton blockchain. Well, many feel the project has full backing from Durov and Musk since they are among the top Dogs meme family in this industry. The launch of Dogs sparks the rise of Cats with many teasing the cats meme community to surpass dogs success. I wonder if this rival could mean anything to TON blockchain but we await the listing price. With CATS about to listed, the project has open deposit so users can choose their respective preferred exchange to trade their token. Currently, only Bitget is available with a 30k $cats airdrop to the first 200k users that deposit while the project team continue working tirelessly to open deposit for the remaining exchanges. I wonder if this will pay for hamsterkombat and catizen failure and list above Dogs FDV.
×
×
  • Create New...
us