Forum > Networking and Web Programming

[Solved] how to send application/x-www-form-urlencoded

(1/1)

BlueIcaro:
Hello, I'm connecting to a server. I have to send a post request using  TFPHTTPClient, with content-type as application/x-www-form-urlencoded.
So I wrote:

--- Code: ---
  PeticionHttp.AddHeader('Content-Type', 'application/x-www-form-urlencoded;v=2.0');
  PeticionHttp.AddHeader('Accept', 'application/hal+json;v=2.0');

  Respuesta := TStringList.Create();
  BodyText := 'resources=1&1=/rw/iosystem/signals/ethernet/board1/do0&1-P=0';
  PeticionHttp.RequestBody := TRawByteStringStream.Create;
  PeticionHttp.RequestBody.WriteAnsiString(BodyText);

  PeticionHttp.Post('https://localhost:80/subscription', Respuesta);   

--- End code ---
The problem is that the server answers: 400 Bad Request
If I make the request using Postman (a small program to test requests) I don“t get any error as you can see in the picture.
I think that the problem is that I'm make same mistake in my code. But I don't know what

Thanks

/BlueIcaro

BlueIcaro:
Hello, I found the solution. The problem was that I don't . So I have to do it on my code.

--- Code: ---  Params:='resources=1&1=/rw/iosystem/signals/ethernet/board1/do0;state&1-p=0';
  PeticionHttp.AddHeader('Content-Type', 'application/x-www-form-urlencoded;v=2.0');
  PeticionHttp.AddHeader('Accept', 'application/xhtml+xml;v=2.0');
  PeticionHttp.AddHeader('Content-Length',IntToStr(Length(Params))); //We need send the length of the body
  PeticionHttp.RequestBody := TRawByteStringStream.Create(Params);
  PeticionHttp.Post('https://localhost:80/subscription',Respuesta);   

--- End code ---

/BlueIcaro

Navigation

[0] Message Index

Go to full version