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条)

报告相同问题?