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

    • With Bitcoin rising above $65K, it's an ideal moment to capitalize on market momentum by joining trading competitions and earning additional income. If you're a crypto enthusiast aiming to sharpen your trading skills, grow your community, and increase your presence in the industry, I suggest exploring Bitget's Builder Program.   This Builder Program offers exclusive perks and benefits to help you build a thriving crypto community while providing access to valuable resources as the bull market gains momentum. As a member, you'll gain recognition in the crypto space, unlock VIP trading features, and connect with traders worldwide.   One of the standout benefits is the chance to trade and earn up to $1,000 weekly. This program presents a great opportunity to enhance your earnings while actively engaging with the market. For more details, you can search for " Bitget Builders harvest time trading battle" on Google or ask some questions in the comments..
    • 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.
×
×
  • Create New...
us