duanji6997
2017-08-11 06:56
浏览 26
已采纳

在Go中查询Magento API

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?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtu1747 2017-08-11 07:13
    已采纳

    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>
          ...
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题