Pass body in GET request

Asked

Viewed 223 times

0

Good morning friends. How can I spend one body in the request GET?

I have an API that does a CPF search in a PDF and returns the page it is on. In the terminal, the command is as follows:

http GET http://127.0.0.1:8000/files/processed/cpf.pdf data="123.456.789-10"

I would like to consume this API with Dart/Flutter, but I cannot pass the body in the request. I tried with the following code:

   class PdfSearch{
      
      Future catchPage(String numero) async {
      Uri uri = Uri.parse("http://127.0.0.1:8000/files/processed/cpf.pdf");
      http.Request request = http.Request("GET", uri);
      request.body = '{"data": $numero}';
      final response = request.send().asStream().first;
    
  }

}

Unsuccessful...

1 answer

1


Endpoints GET originally should not contain body in the request, which is precisely why the method http.get() has not a parameter body. For he is simply ignored.

Future<Response> get(Uri url,{Map<String, String>? headers})

If it is necessary, this may indicate a bad design in the creation of the API. In your case, the use of queryParams:

http://127.0.0.1:8000/files/processed/cpf.pdf?cpf=12345678910

Tools that cache Apis, for example, use the url for this. Thus, it is possible to pass the data in the url using its own code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.