Recent

Author Topic: [Solved] how to send application/x-www-form-urlencoded  (Read 568 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 793
    • Blog personal
[Solved] how to send application/x-www-form-urlencoded
« on: January 17, 2024, 09:23:29 am »
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: [Select]

  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);   
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
« Last Edit: January 17, 2024, 12:37:30 pm by BlueIcaro »

BlueIcaro

  • Hero Member
  • *****
  • Posts: 793
    • Blog personal
Re: how to send application/x-www-form-urlencoded
« Reply #1 on: January 17, 2024, 12:26:47 pm »
Hello, I found the solution. The problem was that I don't . So I have to do it on my code.
Code: [Select]
  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);   

/BlueIcaro

 

TinyPortal © 2005-2018