0

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

Lluis Bernabeu
  • 83
  • 3
  • 11
  • @whosrdaddy it is not a duplicate, the question you linked is about putting it in a stringlist where a stream is needed. This is about the 400 bad-request and the fact that in that case idHTTP doesn't give you back the body that you need. – Pieter B Oct 03 '14 at 08:53
  • 2
    You are missing a comma after the user, should be `'"user": "****",'+#10+` – David A Oct 03 '14 at 08:54
  • the problem is not the comma – Lluis Bernabeu Oct 03 '14 at 09:44
  • You can use the [SuperObject](https://code.google.com/p/superobject/) open source library to create JSON from Delphi. – mjn Oct 04 '14 at 10:32

2 Answers2

1

Now I have the solution:

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

And is working

Lluis Bernabeu
  • 83
  • 3
  • 11
-2

Error 400 is a reply you get when there are errors in your JSON.

I would advise using another component to send your POST request, a component that does give you the body text back, because that's what you need.

Synapse has an alternative to idHTTP.

Pieter B
  • 1,874
  • 10
  • 22
  • 3
    The response text you can get from the `EIdHTTPProtocolException.ErrorMessage` property. I wouldn't suggest using a different library. – TLama Oct 03 '14 at 09:03
  • You get the error respponse, not the body, those are 2 different things. I get the error-response: [no JSON object could be decoded] but it doesn't show the information that's actually usefull : {"user": ["Invalid user"]} – Pieter B Oct 03 '14 at 09:16
  • @PieterB can you include the full HTTP response to illustrate where the two different things are in the response? – mjn Oct 03 '14 at 16:13
  • 1
    I can confirm that `EIdHTTPProtocolException.ErrorMessage` does indeed contain the **body** of the HTTP response. The HTTP reply text (`Bad Request`) is in `EIdHTTPProtocolException.Message`. – Remy Lebeau Oct 03 '14 at 18:38