0

I am using some external API to GET and POST some ressources, locally my code works fine with the call of different endPoints (GET, POST...) and even with Postman, but when i try to run my code in another platerform (where the ressources are), i get the 412 HTTP error due to a POST call : after looking on the internet, i found out that i should generate an ETagd of the entity (that i went to modify) and add it into the header of my POST endPoint. For that, i used ShallowEtagHeaderFilter and the @Bean annotation(above the filter method) and the @SpringBootApplication annotation above my class, here is my code :

package main.Runners;

import io.testproject.java.annotations.v2.Parameter;
import okhttp3.*;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import javax.servlet.Filter;


@SpringBootApplication
public class ActionRunner {

    @Parameter(description = "the project ID")
    public static String projectId = "xxx";

    @Parameter(description = "the test ID")
    public static String testId = "yyy";


    public static void main(String[] args) throws Exception {

        try {

            OkHttpClient client = new OkHttpClient().newBuilder()
                                          .build();
            Request request = new Request.Builder()
                                      .url("https://api.testproject.io/v2/projects/"+projectId+"/tests/"+testId)
                                      .method("GET", null)
                                      .addHeader("Authorization", "nzmo4DI08ykizYgcp9-5cCTArlxq7k7zt9MYhGmTcRk1")
                                      .build();
            Response response = client.newCall(request).execute();

            System.out.println("================ this is our response headers ::: \n"+ response.headers());

        } catch(Exception e) {
            System.out.println(e);
        }
    }

    @Bean
    public ShallowEtagHeaderFilter shallowEtagHeaderFilter(){
        return new ShallowEtagHeaderFilter();
    }
}

I really need Your help since i cant generate any ETag parameter on my GET response header(after checking reponse.headers() ). Thanks in advance!

Im091
  • 11
  • 2
  • "sending a post request"... in the code you are providing, there is a GET call, not POST – Alberto Sinigaglia Jun 07 '21 at 20:16
  • Yes i am providing a GET to retrieve the ETag of the entity (from the response header) and put it in the request header of the POST to avoid 412 in my POST call. Thats why i just put the GET for now since i wanna generate the Etag from it first. THANKS – Im091 Jun 08 '21 at 18:02
  • Have you tried just enabling cache on `OkHttpClient ` – Alberto Sinigaglia Jun 08 '21 at 19:31
  • @AlbertoSinigaglia Thanks for your help by the way :), i have added .addHeader("Cache-Control", "max-age=86400, public") to my GET request, but still got "cache-control: no-cache", and "pragma: no-cache" in my header response :( – Im091 Jun 09 '21 at 12:35
  • I mean like adding `.cacheControl(new CacheControl.Builder().noCache().build())` to the request (in theory, this will disable caching, and so no ETag is required)... do this in the POST Request (and please, post also that code here) – Alberto Sinigaglia Jun 09 '21 at 12:55
  • I have tried your suggestion but I still got the 412 http error on that plateform, and in my IDE it was working fine :/, ok I will post the rest of the code (POST + GET) – Im091 Jun 09 '21 at 21:18

0 Answers0