Error 400 API Call lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- jorlandocorreia · 2022-03-02 09:30 Hello people. I am trying to make a request to a public API (https://fairlay.com/api/, for examples check https://fairlay.com/api/#/public/?id=examples) But I am getting 400 error: "Project raised exception, class 'EHTTPClient' with message: Unexpected response satus code: 400. I know this is related to a malformed request, but this works in the browser and Postman, without any modifications. This is my code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      client:= TFPHTTPClient.Create(nil);     client.AddHeader('Accept- Encoding', 'gzip, deflate');     response := client.Get('http://83.171 .236.114:8080/free/markets/{"Cat":1,"NoZombie":true,"RunnerAND":["Port ugal"], "TitleNOT":["Corners","Throwin"], "PeriodOR":[1],"FromID":0,"ToID":1}');     ShowMessage(response);  Any idea where is the problem? Thanks in advance ---------------------------------------------------------------------- egsuh · 2022-03-02 09:39 Don't you need a function like EncodeURL, which replaces e.g. blank with %20, etc?  I think browser does that, but Lazarus does not. ---------------------------------------------------------------------- Red_prig · 2022-03-02 09:41 Are you sure that: Quote{"Cat":1,"NoZombie":true,"RunnerAND":["Portugal"], "TitleNOT":["Corners","Throwin"], "PeriodOR":[1],"FromID":0,"ToID":1}that this is part of the URL, and not the body of the request? in addition, it is not clear from the code fragment which request method is GET or POST. ---------------------------------------------------------------------- jorlandocorreia · 2022-03-02 09:54 Quote from: egsuh on March 02, 2022, 10:39:03 amDon't you need a function like EncodeURL, which replaces e.g. blank with %20, etc?  I think browser does that, but Lazarus does not. Even using EncodeURLElement(), i get an error of invalid protocol "". I removed the spaces in the string in my code, but still get the 400 error. Quote from: Red_prig on March 02, 2022, 10:41:48 amAre you sure that: Quote{"Cat":1,"NoZombie":true,"RunnerAND":["Portugal"], "TitleNOT":["Corners","Throwin"], "PeriodOR":[1],"FromID":0,"ToID":1}that this is part of the URL, and not the body of the request? in addition, it is not clear from the code fragment which request method is GET or POST. As I can see in the API documentation page, this is not part of the body, it is part of the url. And the method is Get. I used Get and SimpleGet but it always return error 400. ---------------------------------------------------------------------- PierceNg · 2022-03-02 11:27 Quote from: jorlandocorreia on March 02, 2022, 10:54:47 amEven using EncodeURLElement(), i get an error of invalid protocol "". I removed the spaces in the string in my code, but still get the 400 error. You only URL-encode the request, not the 'http://...:8080/' before. So code should look like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i url := '/free/markets/{"Cat":1,"NoZombie":true,"RunnerAND":["Portugal"], "TitleNOT":["Corners","Throwin"], "PeriodOR":[1],"FromID":0,"ToID":1}' ;client.Get('http://83.171.236.114:8080/' + EncodeURLElement(url)); ---------------------------------------------------------------------- jorlandocorreia · 2022-03-02 11:37 Quote from: PierceNg on March 02, 2022, 12:27:18 pmQuote from: jorlandocorreia on March 02, 2022, 10:54:47 amEven using EncodeURLElement(), i get an error of invalid protocol "". I removed the spaces in the string in my code, but still get the 400 error. You only URL-encode the request, not the 'http://...:8080/' before. So code should look like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i url := '/free/markets/{"Cat":1,"NoZombie":true,"RunnerAND":["Portugal"], "TitleNOT":["Corners","Throwin"], "PeriodOR":[1],"FromID":0,"ToID":1}' ;client.Get('http://83.171.236.114:8080/' + EncodeURLElement(url)); Thanks for the help, I tried that, and now I receive a strange symbol when showing the message: (https://i.ibb.co/BPN6mGg/Lazaur.png) The error 400 is not happening now but I am not sure what's happening. If I call other totally different APIs like jsonplaceholder I don't that error. I appreciate the help. ---------------------------------------------------------------------- Thaddy · 2022-03-02 11:41 https? instead of http... ---------------------------------------------------------------------- PierceNg · 2022-03-02 11:52 Quote from: jorlandocorreia on March 02, 2022, 12:37:03 pmQuote from: PierceNg on March 02, 2022, 12:27:18 pmYou only URL-encode the request, not the 'http://...:8080/' before. So code should look like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i url := '/free/markets/{"Cat":1,"NoZombie":true,"RunnerAND":["Portugal"], "TitleNOT":["Corners","Throwin"], "PeriodOR":[1],"FromID":0,"ToID":1}' ;client.Get('http://83.171.236.114:8080/' + EncodeURLElement(url)); Thanks for the help, I tried that, and now I receive a strange symbol when showing the message: (https://i.ibb.co/BPN6mGg/Lazaur.png) The error 400 is not happening now but I am not sure what's happening. If I call other totally different APIs like jsonplaceholder I don't that error. I appreciate the help. The response isn't a string. It is gzip data. You'll have to gunzip it. That's what the 'Accept-Encoding' header is for. Make the request with your web browser, and use the browser's developer console to see what is happening. ---------------------------------------------------------------------- Red_prig · 2022-03-02 12:57 remove the line Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i client.AddHeader('Accept-Encoding', 'gzip, deflate'); should work ---------------------------------------------------------------------- jorlandocorreia · 2022-03-02 14:18 Quote from: Red_prig on March 02, 2022, 01:57:01 pmremove the line Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i client.AddHeader('Accept-Encoding', 'gzip, deflate'); should work The API documentation says I need to include that. But with or without it, I get the same error. Quote from: PierceNg on March 02, 2022, 12:52:11 pmQuote from: jorlandocorreia on March 02, 2022, 12:37:03 pmQuote from: PierceNg on March 02, 2022, 12:27:18 pmYou only URL-encode the request, not the 'http://...:8080/' before. So code should look like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i url := '/free/markets/{"Cat":1,"NoZombie":true,"RunnerAND":["Portugal"], "TitleNOT":["Corners","Throwin"], "PeriodOR":[1],"FromID":0,"ToID":1}' ;client.Get('http://83.171.236.114:8080/' + EncodeURLElement(url)); Thanks for the help, I tried that, and now I receive a strange symbol when showing the message: (https://i.ibb.co/BPN6mGg/Lazaur.png) The error 400 is not happening now but I am not sure what's happening. If I call other totally different APIs like jsonplaceholder I don't that error. I appreciate the help. The response isn't a string. It is gzip data. You'll have to gunzip it. That's what the 'Accept-Encoding' header is for. Make the request with your web browser, and use the browser's developer console to see what is happening. This looks to be the issue, I will check how to unzip the response. Thanks