HTTP error code 400

I have successfully made many requests in the past, but usually to my own web server. I am stuck on this one. The request works fine in Postman, but in my Go code, the server returns an error 400 code. I wish the response could give me a clue on what I need to change.
In Postman, I have set the host to:
https://finnhub.io/api/v1

and then added :
/stock/candle. to the URL line and set it to ‘get’. I have set the params and the URL then shows:
{{host}}/stock/candle?symbol=msft&resolution=D&from=1649887189&to=1658527189
The authorization is set in the collection as:
Key: X-Finnhub-Token
Value: c362xxxxxxxxxuojg (modified for security)
I do a send and it performs as requested. I get a list of stock candles. Nice

However, when I attempt this in Go, I get an error 400.

urlString := fmt.Sprintf("https://finnhub.io/api/v1" + "/stock/candle")

req, err := http.NewRequest("get", urlString, nil)
values := req.URL.Query()
values.Add("symbol", specification.Stock.Symbol)
if specification.TriggerCriteria.FrequencyUnit == data.UnitDay {
	values.Add("resolution", "D")
}
now := time.Now()
nowString := fmt.Sprintf("%v", now.Unix())
oneHundredDaysAgo := time.Hour * 24 * 100 * -1
past := now.Add(oneHundredDaysAgo)
pastString := fmt.Sprintf("%v", past.Unix())
values.Add("from", pastString)
values.Add("to", nowString)
req.URL.RawQuery = values.Encode()

req.Header.Add("X-Finnhub-Token", "c362jxxxxxxx0uojg")

resp, err := http.DefaultClient.Do(req)`

The response has a 400 status code.

Screenshot of the request:

Is there a “console” that you can look at (web browser)? Do it complains about CORS or similar?

I’m using Goland as my IDE. And I can look at any variable and I have but I can’t find anything wrong.

Have you checked the response body? What does it say?

1 Like

The response body is empty.

1 Like

I have not figured out why I get an error code 400. However, I have downloaded Finnhub’s GO SDK and it can retrieve the data just fine. It’s a very complex project and the access is very high-level and I haven’t yet found exactly where they are executing the request. I guess I’ll use the SDK. I would still like to know why I can use the request in Postman but not as a simple request in Go.

I stepped through their code and their request looks just like mine.

SDK:
GET /api/v1/stock/candle?from=1649970549&resolution=D&symbol=AAPL&to=1658610549 HTTP/1.1
Host: finnhub.io
User-Agent: OpenAPI-Generator/2.0.13/go
Accept: application/json
X-Finnhub-Token: cbbb00iad3ibhoa1vbcg
Accept-Encoding: gzip


Mine:
GET /api/v1/stock/candle?from=1649970549&resolution=D&symbol=AAPL&to=1658610549 HTTP/1.1
Host: finnhub.io
User-Agent: OpenAPI-Generator/2.0.13/go
Accept: application/json
X-Finnhub-Token: cbbb00iad3ibhoa1vbcg
Accept-Encoding: gzip

I created these using:

httputil.DumpRequestOut

What is the response from the server? Something like dump, err := httputil.DumpResponse

You said it was empty, but you may find a clue if you can get a response from the server. The server must response in some way. I have played a bit with API in AJAX and had a fair share of CORS errors.

1 Like

hard to know as just from the 400 response.
Can you print out resp.Body which may give you more hints.
e.g.
io.Copy(os.Stdout, res.Body)

Maybe post some complete runnable code, so we can see what is going on…

I just ran the offending code again to check the dump response. This time it succeeded with status 200. And I didn’t change the code. Pretty strange, but I need to move on. I’ll use the SDK as it works just fine every time.
Thanks for your responses.

Interesting.
The code will send the same request every time, except for the lines

which changes on every run. (Also the from param).
Could there be some values that the server objects to?
e.g. will it object to values of to in the future?
Could this be a problem of clock-skew between client and server?
What happens if you ommit the to= term in the request?

I suspect you are right. I may have supplied an invalid to parameter as I was just learning that api.