0

I cannot do a POST using HTTP CLIENT, even I look to all of the headers sets and params on Browser.

That's my trace:

br.com.serasa.captacao.framework.web.http.UnknownHttpResponseHarvestException: POST https://portaldecustas.tjsp.jus.br/portaltjsp/pages/custas/servico/abrir / 400 - Bad Request
    at br.com.serasa.captacao.framework.web.http.HttpHelper.executeHtmlRequest(HttpHelper.java:483)
    at br.com.serasa.captacao.framework.web.http.HttpHelper.executeHtmlRequest(HttpHelper.java:448)
    at br.com.serasa.captacao.framework.web.page.Page.requestHtml(Page.java:98)
    at br.com.fiveware.dare.sp.emissao.guias.captador.page.DareSpPage.enviarFormulario(DareSpPage.java:107)
    at br.com.fiveware.dare.sp.emissao.guias.captador.CaptadorDareSP.enviarFormulario(CaptadorDareSP.java:68)
    at br.com.fiveware.dare.sp.emissao.guias.captador.page.DareSpPageTest.enviarFormulario(DareSpPageTest.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

This's my code:

public void enviarFormulario(DadosContribuinte contribuinte) throws HarvestException

{
    LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();
    params.put("nomeServico", contribuinte.getServico());
    params.put("cpfCnpj", contribuinte.getCnpj());
    params.put("nome", contribuinte.getNome());
    params.put("telefone", contribuinte.getTelefone());
    params.put("endereco", contribuinte.getEndereco());
    params.put("uf", contribuinte.getUf());
    params.put("cidade", contribuinte.getCidade());

    HttpPost post = newPostMethod(
            URI.create("https://portaldecustas.tjsp.jus.br/portaltjsp/pages/custas/servico/abrir"), 6000);


    post.addHeader("Host", "portaldecustas.tjsp.jus.br");
    post.addHeader("Accept", "*/*");
    post.addHeader("Accept-Language", "en-US,en;q=0.5");
    post.addHeader("Accept-Encoding", "gzip, deflate, br");
    post.addHeader("Referer", "https://portaldecustas.tjsp.jus.br/portaltjsp/pages/custas/new");
    post.addHeader("Content-type", "application/json");

    requestHtml(post);
}

These are the request headers (VIA BROWSER):

https://lh3.googleusercontent.com/-4jCNPVZpfJQ/WZwxDZKqtAI/AAAAAAAABTM/tAL9Fx_WCJARitnibC_z_roNHeiIg7SPQCK8BGAs/s512/2017-08-22.png

These are the POST DATA:

{"nomeServico":"estampagem_ou_autenticacao_mecanica","cpfCnpj":"435.248.238-23","nome":"EDERSON DUARTE ITABAIANA JUNIOR","telefone":"(11) 11111-1111","endereco":"teste","uf":"26","cidade":"9422"}
Ederson JR
  • 59
  • 1
  • 9
  • 1
    I see your params are not linked with the post. Try the following before `requestHtml(post)`: `post.setEntity(new UrlEncodedFormEntity(params));` Otherwise see: https://stackoverflow.com/a/12060103/3977134 – r3dst0rm Aug 22 '17 at 13:32
  • Http-400 Bad Request means that your request is badly formatted: it may be, i.e., missing some `@RequestParam` which is required. Would you mind sharing with us your `@RestController` class? – Pijotrek Aug 22 '17 at 13:32
  • yes, you are right. I wasn't using the params, i forgot... Your detail was very helpful... I added this: HttpPost post = newPostMethod(getTarget(), params, getTimeoutHandler()); – Ederson JR Aug 22 '17 at 13:59
  • how can I mark the question how solved? – Ederson JR Aug 22 '17 at 14:01

0 Answers0