Postback JSON broken ?

quadcodes
From today morning I'm facing issues with Postback JSON.

Here is what I receive as JSON

{'{"placed_by":"XX","order_id":"XX","exchange_order_id":"XX","parent_order_id":"XX","status":"COMPLETE","status_message":"","order_timestamp":"2018-08-06 11:30:28","exchange_update_timestamp":"","exchange_timestamp":"2018-08-06 11:30:28","variety":"co","exchange":"NSE","tradingsymbol":"BPCL","instrument_token":134657,"order_type":"LIMIT","transaction_type":"SELL","validity":"DAY","product":"CO","quantity":1,"disclosed_quantity":0,"price":0,"trigger_price":400.45,"average_price":400.4,"filled_quantity":100,"pending_quantity":0,"cancelled_quantity":0,"market_protection":0,"tag":"","guid":"","user_id":"XP9716","unfilled_quantity":0,"app_id":8212,"checksum":"XX"}': '' }

While validating JSON object, It gives following error :smile:
SyntaxError: Unexpected token ' in JSON at position 2

Please fix it asap. My order execution completely rely on postback JSON & I'm facing issues since morning.

@sujith @rakeshr @Kailash
  • sujith
    Postbacks seem fine from our end.
    I don't think we are sending postbacks in the above-mentioned format like enclosed JSON enclosed inside other curly braces.
  • quadcodes
    @sujith I have sent you screenshot of the JSON object as Private Message. Please check.
  • enliyo
    enliyo edited September 2018
    @sujith indeed the postback payload received is malformed JSON. I see what @quadcodes has mentioned on my newly implemented nodejs backend.

    This is the error I get when I try to parse the json in JavaScript with JSON.parse() method.
    SyntaxError: Unexpected token o in JSON at position 1

    Here is the exact payload I received at postback URL route.


    { '{"placed_by":"PLACEHOLDER","order_id":"PLACEHOLDER","exchange_order_id":"","parent_order_id":"","status":"REJECTED","status_message":"RMS:Blocked for OPTSTK MKT nse_fo broker- ZERODHA Remarks: Option Stock market orders are not allowed block type: ALL","order_timestamp":"2018-09-22 12:07:40","exchange_update_timestamp":"","exchange_timestamp":"","variety":"regular","exchange":"NFO","tradingsymbol":"MARUTI18SEP8500CE","instrument_token":19073538,"order_type":"MARKET","transaction_type":"BUY","validity":"DAY","product":"NRML","quantity":75,"disclosed_quantity":0,"price":0,"trigger_price":0,"average_price":0,"filled_quantity":0,"pending_quantity":0,"cancelled_quantity":0,"market_protection":0,"tag":"","guid":"PLACEHOLDER","user_id":"PLACEHOLDER","unfilled_quantity":0,"app_id":PLACEHOLDER,"checksum":"PLACEHOLDER"}': '' }



    If you observe carefully, the initial { ' part and ending part ': '' } are causing JSON to be malformed.

    I am not able to proceed because I expect the API payload to be well formed for further processing such as request authentication. Have you not received similar reports from others? I am bit surprised that this is broken since more than 1 month.

    Please fix.
  • enliyo
    @sujith I am receiving similar error that @quadcodes is receiving on my newly implemented Node.JS backend.

    There is what I get
    SyntaxError: Unexpected token o in JSON at position 1

    The payload received on backend for postback URL:
    { '{"placed_by":"PLACEHOLDER","order_id":"PLACEHOLDER","exchange_order_id":"","parent_order_id":"","status":"REJECTED","status_message":"RMS:Blocked for OPTSTK MKT nse_fo broker- ZERODHA Remarks: Option Stock market orders are not allowed block type: ALL","order_timestamp":"2018-09-22 12:07:40","exchange_update_timestamp":"","exchange_timestamp":"","variety":"regular","exchange":"NFO","tradingsymbol":"MARUTI18SEP8500CE","instrument_token":19073538,"order_type":"MARKET","transaction_type":"BUY","validity":"DAY","product":"NRML","quantity":75,"disclosed_quantity":0,"price":0,"trigger_price":0,"average_price":0,"filled_quantity":0,"pending_quantity":0,"cancelled_quantity":0,"market_protection":0,"tag":"","guid":"PLACEHOLDER","user_id":"PLACEHOLDER","unfilled_quantity":0,"app_id":PLACEHOLDER,"checksum":"PLACEHOLDER"}': '' }

    If you notice carefully in payload, the starting portion '{" and ending portion ': '' } need not be there and are causing JSON to be malformed.

    I am surprised that no one has reported this and probably that's why this is still broken. Can you please look into it?
  • sujith
    Is it consistent?
    We just tried and it seems fine.
    Can you mention, if you are using some third party library or give a brief description of the implementation here?
  • pawan
    If someone still stumbles upon this problem -
    Postback event received on webhook is having Content-Type set as application/x-www-form-urlencoded. If you're expecting application/json, you'll be having parsing issues like above.
    1. Set the content-type for webhook as application/x-www-form-urlencoded and make your API compatible for it., or
    2. If you're using expressjs, implement a middleware which does this parsing job for you, or
    3. If you're using loopbackjs (specifically), add this line inside start function in your server.js file


    app.use(function (req, res, next) {
    if (req.headers['content-type'] === 'application/x-www-form-urlencoded') {
    req.headers['content-type'] = 'application/json';
    }
    next();
    });

  • ramesh33
    ramesh33 edited April 2020
    @pawan Thank you for sharing your inputs. Knowing the content-type helped me in resolving my issue.
This discussion has been closed.