0

Im trying to call a POST Endpoint that requires an API-Key using RestTemplate but i keep receiving a 403 Forbidden HttpClientErrorException. GET with the same authorization headers works fine so I think the API-Key auth works. The only time I get a 403 on the same endpoint using Postman is if I don't send a body but when I output the HttpEntities body it's not null. My only guess is that I made a mistake with the HttpEntity and the restTemplate.exchange somehow doesn't send a body:

Heres the code: 

    package at.acl.articledata.connector.smokeTest;


import at.acl.articledata.connector.pojos.request.Article;
import com.fasterxml.jackson.databind.util.JSONPObject;
import io.swagger.v3.core.util.Json;
import jakarta.annotation.PostConstruct;
import org.apache.coyote.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


import java.math.BigDecimal;
import java.util.Collections;

@Component
public class SmokeTest {

    @Autowired
    RestTemplate restTemplate = new RestTemplate();

    @PostConstruct
    private void floodDatabase(){
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.add("X-API-KEY", "myApiKey");

        JSONObject jsonArticle = new JSONObject();
        jsonArticle.put("ext_article_nr", "e001");
        jsonArticle.put("ean_numeric", BigDecimal.valueOf(420));
        jsonArticle.put("intern_art_nr", "i001");
        jsonArticle.put("article_name", "Flip Flops");
        jsonArticle.put("size", "45");
        jsonArticle.put("color", "Green");
        jsonArticle.put("group", "No Group");
        jsonArticle.put("brand", "Gucci");
        jsonArticle.put("season", "Summer");
        jsonArticle.put("article_link", "http://dummyimage.com/102x100.png/dddddd/000000");

        JSONArray jsonArticleArray = new JSONArray();
        jsonArticleArray.put(jsonArticle);

        JSONObject jsonBody = new JSONObject();
        jsonBody.put("count", 1);
        jsonBody.put("total_count", 8);
        jsonBody.put("data", jsonArticleArray);

        HttpEntity<JSONObject> request = new HttpEntity<>(jsonBody, headers);

        ResponseEntity<String> response = restTemplate.exchange("correctServerIP", HttpMethod.POST, request, String.class);
        System.out.println("Post Response: " + response);
    }
}

Any help would be appreciated

  • have you tried inspecting what is sent in the request body? `jsonBody` is a `JSONObject`, so Spring will try to use an `HttpMessageConverter` to convert it to a `String`. Also, try to create your request with `new HttpEntity<>(jsonBody.toString(), headers);` – user2340612 Jul 24 '23 at 11:50
  • Thanks for the reply. When i output the request body it seems to be in JSON format: {"data":[{"size":"45","color":"Green","ext_article_nr":"e001","intern_art_nr":"i001","ean_numeric":420,"article_name":"Flip Flops","season":"Summer","brand":"Gucci","group":"No Group","article_link":"http://dummyimage.com/102x100.png/dddddd/000000"}],"total_count":8,"count":1} – Splatted I0I Jul 25 '23 at 06:39

0 Answers0