0

I'm trying to POST a XML body to a HTTP endpoint with RESTTemplate, however the following exception is being thrown - "org.springframework.web.client.RestClientException: Cannot extract response: no Content-Type found".

I have checked and the XML file is being posted to the endpoint successfully, however I cannot resolve this exception - clearly I'm doing something wrong. I think I may need to use a HTTP Mapping Converter , but I don't know how to implement this.

def post(String jenkinsURL, String username, String apiCredentials, String jobName) {

    RestTemplate rest = new RestTemplate()
    String url = "http://$jenkinsURL//createItem?name=$jobName"
    def jenkinsConfigPath = "src/main/resources/JenkinsConfig.xml"
    def encoding = Base64.getEncoder().encodeToString((username + ":" + apiCredentials).getBytes())
    String xmlConfigFile = jenkinsConfigReader.read(jenkinsConfigPath)

    HttpHeaders headers = new HttpHeaders()
    headers.setContentType(MediaType.APPLICATION_XML)
    headers.add("Authorization", "Basic " + encoding)

    HttpEntity<String> entity = new HttpEntity<String>(xmlConfigFile, headers)

    rest.exchange(url, HttpMethod.POST, entity, String.class)
Matty
  • 21
  • 8
  • I figure it out. I've changed the version of org.springframework i was using to "compile group: 'org.springframework', name: 'spring-web', version: '4.3.7.RELEASE'" when compiling with Gradle. And now I'm not having any issues with the same syntax. Not sure whether this is the best way to do it! – Matty Nov 02 '17 at 11:16

1 Answers1

0

In Java I wrote a filter like that:

@Provider
public class CorsFilter implements ContainerResponseFilter
{
    @Override
    public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext)
            throws IOException
    {
        responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
        responseContext.getHeaders().add(
                "Access-Control-Allow-Headers",
                "origin, content-type, accept, authorization, X-Requested-With");
        responseContext.getHeaders().add("Access-Control-Allow-Credentials", "false");
        responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
        if(requestContext.getMethod().equals("OPTIONS"))
        {
            responseContext.setStatus(200);
        }
    }

}

Providers are a simply a way of extending and customizing the JAX-RS runtime. Maybe this is a way also for you to solve your problem.

pwain
  • 279
  • 2
  • 7