alpacahq / Alpaca-API

The Alpaca API is a developer interface for trading operations and market data reception through the Alpaca platform.
https://alpaca.markets/
142 stars 13 forks source link

Request body format is invalid #89

Open lvca opened 4 years ago

lvca commented 4 years ago

Posting this JSON:

{"symbol":"AUPH","side":"sell","time_in_force":"day","extended_hours":true,"qty":"2211","type":"limit","limit_price":"5.88"}

To the endpoint: https://api.alpaca.markets/v2/orders returns this error:

{"code":40010000,"message":"request body format is invalid"}

I can't see whay it's invalid.

lvca commented 4 years ago

Also, if you guys fina a field is missing or a value is wrong, why don't you include this message in the error? it would save a lot of time to both you and us (algo-trader). Thanks

umitanuki commented 4 years ago

I tried the same body and it worked.

$ curl https://api.alpaca.markets/v2/orders -H "APCA-API-KEY-ID: $APCA_API_KEY_ID" -H "APCA-API-SECRET-KEY: $APCA_API_SECRET_KEY" -d '
{"symbol":"AUPH","side":"sell","time_in_force":"day","extended_hours":true,"qty":"2211","type":"limit","limit_price":"5.88"}
'
{"id":"c1d7167f-81b4-46db-af3e-9809c282e869","client_order_id":"26d62dd2-4807-403a-96ad-4901d0e7e119","created_at":"2019-11-20T06:49:45.214827487Z","updated_at":"2019-11-20T06:49:45.222468037Z","submitted_at":"2019-11-20T06:49:45.179754734Z","filled_at":null,"expired_at":null,"canceled_at":null,"failed_at":null,"replaced_at":null,"replaced_by":null,"replaces":null,"asset_id":"e7ead0e7-11f9-4be6-8668-c9a23a5a498a","symbol":"AUPH","asset_class":"us_equity","qty":"2211","filled_qty":"0","filled_avg_price":null,"order_type":"limit","type":"limit","side":"sell","time_in_force":"day","limit_price":"5.88","stop_price":null,"status":"new","extended_hours":true}
umitanuki commented 4 years ago

Agree the error message could be better. The error is coming from the json parser though which we don't have much control.

d-e-s-o commented 4 years ago

Seems a bit strange to me to throw the hands in the air and state that because the JSON parser is a third party component you can't do much. If your API is flaky that's a bit of an issue, would you agree? I suppose people out there are actually considering relying on your APIs. So random requests failing spuriously could potentially be a problem. Do you see what I am saying?

And if it's a third party component "though [sic] which [you] don't have much control" then perhaps you should consider changing it to something you understand or can observe properly. Does this sound like a reasonable suggestion to you?

awoerp commented 4 years ago

Just in-case it is helpful to anyone else running into this, I was having this issue as well and my problem was that I was supplying the kwarg "data" instead of "json" in my post request.

Python code that didn't work: requests.post(url, headers = self.headers, data = data)

Python code that did work: requests.post(url, headers = self.headers, json = data)

Obviously the error is pretty vague so your issue could be any number of other things. Maybe this will save someone some time.

vijay-r commented 3 years ago

Getting same error

{
  "code": 40010000,
  "message": "request body format is invalid"
}

Code:

const buyMarket = async ({ symbol, qty }) => {
  try {
    const rsp = await fetch(`https://paper-api.alpaca.markets/v2/orders`, {
      method: 'post',
      body: {
        symbol: 'PYPL',
        qty: 1,
        side: 'buy',
        type: 'market',
        time_in_force: 'day',
      },
      headers: headers,
    })
    return rsp.json()
  } catch (error) {
    console.log(error)
  }
}
coodoo commented 3 years ago

Just make sure you JSON.stringify the POST body as below, everything should be fine 😉

const buyMarket = async ({ symbol, qty }) => {
  try {
    const rsp = await fetch(`https://paper-api.alpaca.markets/v2/orders`, {
      method: 'post',
      body: JSON.stringify({
        symbol: 'PYPL',
        qty: 1,
        side: 'buy',
        type: 'market',
        time_in_force: 'day',
      }},
      headers: headers,
    })
    return rsp.json()
  } catch (error) {
    console.log(error)
  }
}
Eram101 commented 8 months ago

what should i do in this code here Alpaca API Response: b'{"code":40010000,"message":"request body format is invalid"}'

def create_order(pred_price, company, test_loss, appro_loss): open_price, close_price = pred_price[0], pred_price[1] if open_price > close_price: side = 'sell' elif open_price < close_price: side = 'buy'

if side == 'buy':
    order = {
        'symbol': company,
        'qty': round(20 * (test_loss / 100)),
        'type': 'stop_limit',
        'time_in_force': 'day',
        'side': 'buy',
        'take_profit': close_price + appro_loss[1],  # Update appro_loss indexing
        'stop_loss': close_price - appro_loss[0]  # Update appro_loss indexing
    }
elif side == 'sell':
    order = {
        'symbol': company,
        'qty': round(20 * (test_loss / 100)),
        'type': 'stop_limit',
        'time_in_force': 'day',
        'side': 'sell',
        'take_profit': close_price - appro_loss[1],  # Update appro_loss indexing
        'stop_loss': close_price + appro_loss[0]  # Update appro_loss indexing
    }

r = requests.post(ORDERS_URL, json=order, headers=HEADERS)
print("Alpaca API Response:", r.content)