Search code examples
jsondelphivcl

HTTP/1.1 400 Bad Request Delphi VCL XE6


I have to send by post to a URL string of JSON text, I use a TidHTTP component for sending by post as shown in the code below, but when I hit the submit button I skip this error, I've been searching google to another people that this happens but none of the solutions have helped me in this case.

var
postData:String;
url:String;
contestacion:String;
datosPOst:TStringList;
lista:TMemoryStream;
begin
datosPOst:=TStringList.Create;
datosPOst.Text:='{'+#10+'"ticketCode": "asdasd",'+#10+
'"CIF": "***",'+#10+
'"storeCode": "***",'+#10+
'"saleTimeStamp": "\/Date(123456000000)\/",'+#10+
'"email": "prueba@***",'+#10+
'"userCP": "30280",'+#10+
'"products": ['+#10+
'{'+#10+
'"Amount": 1,'+#10+
'"Description": "PRUEBA ticket",'+#10+
'"Name": "Prueba ticket 2",'+#10+
'"Price": 25.25,'+#10+
'"ProductCode": "50",'+#10+
'"Tax": 21,'+#10+
'"UnitPrice": 250.00'+#10+
'}'+#10+
'],'+#10+
'"user": "****",'+#10+
'"password": "****"'+#10+
'}';
lista:=TMemoryStream.Create;
datosPOst.SaveToStream(lista);
contestacion:=conexion.Post('http://****.com/Ticket.svc/rest/InsertTicket',lista);

Also if there is any way to form a json from delphi also would appreciate

Thanks


Solution

  • Now I have the solution:

    conexion.Request.ContentType:='application/json';
    

    And is working