ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.59k stars 740 forks source link

Error on Executing HTTPClient Invocation #4159

Closed edgars closed 6 years ago

edgars commented 6 years ago

Hi People,

I tried to execute the following API url invocation:

http://api.bcb.gov.br/dados/serie/bcdata.sgs.1/dados/ultimos/1?formato=json

I had written the following service:

package mine;

import ballerina.net.http;

@http:configuration {basePath:"/dolar"}
service<http> dolar {
    endpoint

     <http:HttpClient> httpConnector{
        create http:HttpClient("http://api.bcb.gov.br", {});}

    http:HttpConnectorError err;

    @http:resourceConfig {
        methods:["GET"],
        path:"/cotacao"
    }
    resource cotacao (http:Request req, http:Response res) {

        http:Response clientResponse = {};
        clientResponse, err = httpConnector.get("/dados/serie/bcdata.sgs.1/dados/ultimos/1?formato=json", req);
        println("GET request: from Dolar Service Banco Central Brasil");
        res.forward(clientResponse);  
    }

}

The return execution returns a 302 HTTP return code, and no response, besides I get an error in the Composer saying: Unexpected error occurred while sending request. Make sure you have entered valid request details.

Any idea of why is it working in the way?

pubudu91 commented 6 years ago

Hi @edgars

This is an issue we are currently looking into. Basically, in a scenario like this, the host header is not properly set when forwarding the incoming request to the back-end. For now, as a workaround, you can explicitly set the host header in the code.

    @http:resourceConfig {
        methods:["GET"],
        path:"/cotacao"
    }
    resource cotacao (http:Request req, http:Response res) {
        http:Response clientResponse = {};
        req.setHeader("Host", "api.bcb.gov.br"); // explicitly set the host header
        clientResponse, err = httpConnector.get("/dados/serie/bcdata.sgs.1/dados/ultimos/1?formato=json", req);
        println("GET request: from Dolar Service Banco Central Brasil");
        res.forward(clientResponse);  
    }

Related issue: #3203

pubudu91 commented 6 years ago

Hi @edgars

We have fixed the issue via PR #4166 and it is no longer required to set the host header explicitly.

shafreenAnfar commented 6 years ago

As per the above comment, I am closing the issue. Thanks for reporting the issue.