'Getting illegal character in scheme name at index 0 error while calling a post request through apache client

I am trying to execute an apache client httppost call in my java code. The url which I was able to execute successfully using curl command was as below-

curl --insecure -ksS -f -u ":Token" -X POST  "https://serverurl/rest/datasource/imports/1234567?pretty=true" -F "query=select * from empschema.employee_table"

I wrote a java code to generate similar URL as that of curl command url

"https://serverurl/rest/datasource/imports/1234567?pretty=true" -F "query=select * from empschema.employee_table"

and I am using the apache client HTTP post method. But I keep on getting the error as below.

java.net.URISyntaxException: Illegal character in scheme name at index 0: "https://serverurl/rest/datasource/imports/1234567?pretty=true" -F "query=select * from empschema.employee_table"

Can someone tell me what exactly is the problem with my code? I even tried to remove " present at the start and end of url and generated a url as below

https://serverurl/rest/datasource/imports/1234567?pretty=true" -F "query=select * from empschema.employee_table

but upon calling httpclient post call it throws an error as

java.net.URISyntaxException: Illegal character in query at index 111:

package com.controller;

@RestController
public class Test {
    @RequestMapping(value = "/query/{queryId}", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "This API posts a new dataset using query")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully added new dataset details"),
            @ApiResponse(code = 404, message = "Unable to add new dataset details"),
            @ApiResponse(code = 500, message = "Internal server error") })

    public InputStream DatasourceImport() throws ClientProtocolException,Exception {

        String baseUrl="\"https://serverurl/rest/datasource/imports/";
        String dataSourceId="1234567";
        String query = "select * from empschema.employee_table\"";
        String fchar = " -F ";

        String url = baseUrl + dataSourceId + "?pretty=true\"" + fchar + "\"query=" + query;

        HttpClient client = HttpClientBuilder.create().build();


        HttpPost postRequest = new HttpPost(url);
        String auth = ":8f5bbe9382f0418d9cf9e5637e177a75";
        byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("UTF-8")));
        String authHeader = "Basic " + new String(encodedAuth);
        postRequest.addHeader(HttpHeaders.AUTHORIZATION, authHeader);

        HttpResponse response = client.execute(postRequest);
        HttpEntity entity = new BufferedHttpEntity(response.getEntity());
        return entity.getContent();

    }
}

I want to successfully execute the post call and load the data using my http post method of apache client.



Solution 1:[1]

Please remove white space(extra spaces) from your URL.

You can also use:

url.trim();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shubham Vyas