I am building a service in Go that queries a Magento API.
I already have the oauth credentials needed to make the request (these are persistent) and am able to successfully query the API in Postman.
I am trying to query the Magento API using this package, however every time I make the request I get an error:
Service temporary unavailable
I have searched around and it looks like this is a common error to get when the request does not have a header for Accept: application/json
.
I am using this package to sign my requests currently and cannot see any way to add this header. I am open to using a different package if required, it just needs to support oauth1 authentication.
Being relatively new to Go, I'm not too sure how to add the header to my request and would love some help.
This is my current code:
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/dghubble/oauth1"
)
func main() {
config := oauth1.NewConfig("consumer key", "consumer secret")
token := oauth1.NewToken("token key", "token secret")
httpClient := config.Client(oauth1.NoContext, token)
path := "https://www.example.com/api/rest/customers?limit=2&order=created_at&dir=DESC"
resp, err := httpClient.Get(path)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Raw Resonse Body:
%v
", string(body))
}
How can I add the Accept: application/json
header to my request?
Create a request:
req, err := http.NewRequest("GET", path, nil)
if err != nil {
// handle error
}
Set the headers:
req.Header.Add("Accept", "application/json")
Run the request using client as configured in the question:
resp, err := httpClient.Do(req)
if err != nil {
// handle error
}
Example that worked for me:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/dghubble/oauth1"
)
func main() {
config := oauth1.NewConfig("consumer key", "consumer secret")
token := oauth1.NewToken("token key", "token secret")
httpClient := config.Client(oauth1.NoContext, token)
path := "https://www.example.com/api/rest/customers?limit=2&order=created_at&dir=DESC"
req, err := http.NewRequest("GET", path, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Accept", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("Raw Resonse Body:
%v
", string(body))
}
Output:
Raw Resonse Body:
<!doctype html>
<html>
<head>
<title>Example Domain</title>
...
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
...
I finally have this working after countless hours spent on it - I swapped the oauth package from dghubble/oauth1 to nhjk/oauth.
This uses the native Go http
package and so we can set headers like normal, and as outlined by @AlexEfimov.
Working code as below:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/nhjk/oauth"
)
func main() {
ck := "consumer key"
cs := "consumer secret"
tk := "token key"
ts := "token secret"
// create an http client and a request for it to send
client := new(http.Client)
req, _ := http.NewRequest("GET", "https://www.example.com/api/rest/customers?limit=2&order=created_at&dir=DESC", nil)
req.Header.Set("Accept", "application/json")
// a consumer allows you to authorize requests with a token
cons := oauth.Consumer{ck, cs}
// authorize request
cons.Authorize(req, &oauth.Token{tk, ts})
// send request and print body
res, _ := client.Do(req)
body, _ := ioutil.ReadAll(res.Body)
fmt.Printf("%s
", body)
}