How to perform a GET request with application/x-www-form-urlencoded content-type in Go?

funghorn :

Basically, I need to implement the following method in Go - https://api.slack.com/methods/users.lookupByEmail.

I tried doing it like this:

import (
    "bytes"
    "encoding/json"
    "errors"
    "io/ioutil"
    "net/http"
)

type Payload struct {
    Email string `json:"email,omitempty"` 
}

// assume the following code is inside some function

client := &http.Client{}
payload := Payload{
    Email: "[email protected]",
}

body, err := json.Marshal(payload)
if err != nil {
    return "", err
}

req, err := http.NewRequest("GET", "https://slack.com/api/users.lookupByEmail", bytes.NewReader(body))
if err != nil {
    return "", err
}

req.Header.Add("Authorization", "Bearer "+token)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, err := client.Do(req)
if err != nil {
    return "", err
}

defer resp.Body.Close()
if resp.StatusCode != 200 {
    t, _ := ioutil.ReadAll(resp.Body)
    return "", errors.New(string(t))
}

responseData, err := ioutil.ReadAll(resp.Body)
if err != nil {
    return "", err
}

return string(responseData), nil

But I get an error that "email" field is missing, which is obvious because this content-type does not support JSON payload: {"ok":false,"error":"invalid_arguments","response_metadata":{"messages":["[ERROR] missing required field: email"]}} (type: string)

I couldn't find how to include a post form with the GET request - there is no available post form argument neither to http.NewRequest, nor to http.Client.Get; http.Client.PostForm issues a POST request but GET is needed in this case. Also, I think I have to use http.NewRequest here (unless another approach exists) because I need to set the Authorization header.

Зелёный :

You misunderstand the application/x-www-form-urlencoded header, you should pass an URL parameters here. Check out an example:

import (
  ...
  "net/url"
  ...
)

data := url.Values{}
data.Set("email", "[email protected]")
data.Set("token", "SOME_TOKEN_GOES_HERE")


r, _ := http.NewRequest("GET", "https://slack.com/api/users.lookupByEmail", strings.NewReader(data.Encode()))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to send post request with x-www-form-urlencoded body

Why does Axios send my POST request with Content-Type application/x-www-form-urlencoded when using a string as the request body?

How to fix validation error getting from openapi go code generator for an API which consumes application/x-www-form-urlencoded content type?

Http Put request with content type application/x-www-form-urlencoded not working in Spring

Android Retrofit: content type as application/x-www-form-urlencoded

How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel

how to post data in node.js with content type ='application/x-www-form-urlencoded'

How do I post data using okhttp library with content type x-www-form-urlencoded?

file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded with imgur API

Swift - How to send POST request with "x-www-form-urlencoded" content-type

How is Laravel decoding HTTP request body Content-Type: application/x-www-form-urlencoded when using api call

How do I get raw request body using servicestack with content-type set to application/x-www-form-urlencoded?

How to POST content as application/x-www-form-urlencoded

Sending post request from axios with "Content-Type" : "application/x-www-form-urlencoded" gives an 401 Unauthorized response

Content-Type = 'application/x-www-form-urlencoded' in request is changed to Content-Type: application/json; in Karate version 0.9.2

my request failed when the post 'content-type' is application/x-www-form-urlencoded and * form field param= { <this is a json object>}

How to send a POST request with Content-Type "application/x-www-form-urlencoded"

How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded

How to send post request with content-type x-www-form-urlencoded android retrofit

RestAssured : annot determine how to serialize content-type application/x-www-form-urlencoded. How to create request with key/value structure?

Order of request parameters for content-type application/x-www-form-urlencoded in Spring MVC

Akka HTTP how to POST singleRequest with Content-Type application/x-www-form-urlencoded

How to escape + in post call content type as application/x-www-form-urlencoded

How test Post request with custom object in content type application/x-www-form-urlencoded?

How to get data from webhook with content-type: application/x-www-form-urlencoded;charset=UTF-8?

How to send x-www-form-urlencoded in a post request in webclient?

How to solve error at xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")?

file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded

How to add "application/x-www-form-urlencoded" as Content-type in .Net httpClient