3

I'm working on a library for an API. There's an API endpoint (POST) which when you issue a curl command is:

curl -H "X-API-TOKEN: API-TOKEN" 'http://interest-graph.getprismatic.com/text/topic' \
  --data "title=Clojure" \
  --data "body=Clojure is a dynamic programming language that targets the Java Virtual Machine (and the CLR, and JavaScript). It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming.Clojure is a compiled language - it compiles directly to JVM bytecode, yet remains completely dynamic. Every feature supported by Clojure is supported at runtime."

I'm trying to perform the above command using http.NewRequest:

   var jsonReq = []byte(`{"title": "Clojure", "body": "Clojure is a dynamic programming language that targets the Java Virtual Machine 
        (and the CLR, and JavaScript). It is designed to be a general-purpose language, 
        combining the approachability and interactive development of a 
        scripting language with an efficient and robust infrastructure for multithreaded programming.
        Clojure is a compiled language - it compiles directly to JVM bytecode, 
        yet remains completely dynamic. Every feature supported by Clojure is supported at runtime."}`)

    buf := new(bytes.Buffer)

    err := json.NewEncoder(buf).Encode(jsonReq)
    if err != nil {
        fmt.Println(err)
    }

    u := "http://interest-graph.getprismatic.com/text/topic"
    ApiToken := "API-TOKEN"
    req, err := http.NewRequest("POST", u, buf)
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("X-API-TOKEN", ApiToken)

    if err != nil {
        log.Fatal(err)
    }
    c := http.DefaultClient
    resp, err := c.Do(req)
    if err != nil {
        log.Fatal(err)
    }

    defer resp.Body.Close()

    r, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(string(r))

Not sure if I'm doing this right, because I keep getting an error 500. Any idea what I'm doing wrong?

4
  • I am pretty sure we need more details. What is in err? Also, what does the console say? Any errors? Commented Feb 20, 2015 at 9:08
  • Is it me or you're are trying to encode something that is already JSON? try with just buf := bytes.NewBuffer(jsonReq) Commented Feb 20, 2015 at 9:15
  • 1
    Sending some naked JSON in the response body is something completely different than what curl's --data does. You'll have to set the content type to application/x-www-form-urlencoded and you'll have to send your two values url-encoded. There is no magic happening here, your jason request body is not magically converted to a form post, you'll have to do that yourself. Commented Feb 20, 2015 at 9:22
  • Volker is definitely right, I didn't notice the --data flag in the curl command line. Commented Feb 20, 2015 at 9:29

1 Answer 1

7

The correct code:

package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "net/url"
)

func main() {

    u := "http://interest-graph.getprismatic.com/text/topic"
    ApiToken := "API-CODE"

    data := url.Values{}
    data.Set("title", "Clojure")
    data.Set("body", `Clojure is a dynamic programming language that targets the Java Virtual Machine 
        (and the CLR, and JavaScript). It is designed to be a general-purpose language, 
        combining the approachability and interactive development of a 
        scripting language with an efficient and robust infrastructure for multithreaded programming.
        Clojure is a compiled language - it compiles directly to JVM bytecode, 
        yet remains completely dynamic. Every feature supported by Clojure is supported at runtime.`)

    b := bytes.NewBufferString(data.Encode())

    req, err := http.NewRequest("POST", u, b)
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Set("X-API-TOKEN", ApiToken)

    if err != nil {
        log.Fatal(err)
    }
    c := http.DefaultClient
    resp, err := c.Do(req)
    if err != nil {
        log.Fatal(err)
    }

    defer resp.Body.Close()

    r, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(string(r))

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.