400错误请求,针对frappe.cloud API。

huangapple go评论86阅读模式
英文:

400 Bad Request for frappe.cloud API

问题

我在使用golang代码中的http.NewRequest调用frappe.cloud API时遇到了400 Bad Request错误,但是当我使用postman检查该API时,它是正常工作的。以下是API的链接:

https://xxxx.frappe.cloud/api/resource/Item?fields=["name","item_name","item_group","description"]&filters=[["Item","item_group","=","xxx Product"]]

如果我使用相同的golang代码调用没有过滤器的相同API,它可以正常工作。以下是正常工作的API:

https://xxxx.frappe.cloud/api/resource/Item?fields=["name","item_name","item_group","description"]

代码如下:

func FetchProperties(dataChannel models.DataChannel) (map[string]interface{}, error) {
    thisMap := make(map[string][]map[string]interface{})
    client := &http.Client{}
    req, err := http.NewRequest("GET", dataChannel.APIPath, nil)
    if err != nil {
        commons.ErrorLogger.Println(err.Error())
        return nil, err
    }
    eds, err := GetDecryptedEDSByEDSID(dataChannel.EDSId)
    if err != nil {
        commons.ErrorLogger.Println(err.Error())
        return nil, &commons.RequestError{StatusCode: 400, Err: err}
    }
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Authorization", eds.DataSource.Auth.Token)
    response, err := client.Do(req)
    if err != nil {
        commons.ErrorLogger.Println(err.Error())
        return nil, err
    }
    data, err := ioutil.ReadAll(response.Body)
    if err != nil {
        commons.ErrorLogger.Println(err.Error())
        return nil, &commons.RequestError{StatusCode: 400, Err: err}
    }
    if response.StatusCode == 200 {
        err = json.Unmarshal(data, &thisMap)
        if err != nil {
            commons.ErrorLogger.Println(err.Error())
            return nil, &commons.RequestError{StatusCode: 400, Err: err}
        }
        return thisMap["data"][0], err
    } else {
        return nil, &commons.RequestError{StatusCode: response.StatusCode, Err: errors.New("getting " + strconv.Itoa(response.StatusCode) + " From Data channel API")}
    }
}

希望对你有所帮助!

英文:

I'm getting 400 Bad Request for frappe.cloud API, when I'm trying to call it using golang code using http.NewRequest, this API is working fine when I check it using postman. following is the API

https://xxxx.frappe.cloud/api/resource/Item?fields=["name","item_name","item_group","description"]&filters=[["Item","item_group","=","xxx Product"]]

If I use the same golang code to call same API with out filters it works fine. following is the working API

https://xxxx.frappe.cloud/api/resource/Item?fields=["name","item_name","item_group","description"]

code as follows

func FetchProperties(dataChannel models.DataChannel) (map[string]interface{}, error) {
thisMap := make(map[string][]map[string]interface{})
client := &http.Client{}
req, err := http.NewRequest("GET", dataChannel.APIPath, nil)
if err != nil {
commons.ErrorLogger.Println(err.Error())
return nil, err
}
eds, err := GetDecryptedEDSByEDSID(dataChannel.EDSId)
if err != nil {
commons.ErrorLogger.Println(err.Error())
return nil, &commons.RequestError{StatusCode: 400, Err: err}
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", eds.DataSource.Auth.Token)
response, err := client.Do(req)
if err != nil {
commons.ErrorLogger.Println(err.Error())
return nil, err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
commons.ErrorLogger.Println(err.Error())
return nil, &commons.RequestError{StatusCode: 400, Err: err}
}
if response.StatusCode == 200 {
err = json.Unmarshal(data, &thisMap)
if err != nil {
commons.ErrorLogger.Println(err.Error())
return nil, &commons.RequestError{StatusCode: 400, Err: err}
}
return thisMap["data"][0], err
} else {
return nil, &commons.RequestError{StatusCode: response.StatusCode, Err: errors.New("getting " + strconv.Itoa(response.StatusCode) + " From Data channel API")}
}

答案1

得分: 1

Postman有一个选项可以将请求转换为等效的编程语言代码。

以下是用于发送请求的工作中的Go代码:

package main

import (
  "fmt"
  "strings"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "https://xxx.frappe.cloud/api/resource/Item?fields=%5B%22name%22,%22item_name%22,%22item_group%22,%22description%22%5D&filters=%5B%5B%22Item%22,%22item_group%22,%22=%22,%22xxx%20Product%22%5D%5D%0A"
  method := "GET"

  payload := strings.NewReader(`{
    "payload": {},
    "url_key": "",
    "req_type": ""
}`)

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Content-Type", "application/json")
  req.Header.Add("Cookie", "full_name=foo; sid=secret_sid; system_user=yes; user_id=foobar; user_image=")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}

以上是将请求转换为Go代码的示例。

英文:

Postman has an option to convert request to programming language equivalent.

Here is a working go code for sending the request. package main

import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "https://xxx.frappe.cloud/api/resource/Item?fields=%5B%22name%22,%22item_name%22,%22item_group%22,%22description%22%5D&filters=%5B%5B%22Item%22,%22item_group%22,%22=%22,%22xxx%20Product%22%5D%5D%0A"
method := "GET"
payload := strings.NewReader(`{
"payload": {},
"url_key": "",
"req_type": ""
}`)
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Cookie", "full_name=foo; sid=secret_sid; system_user=yes; user_id=foobar; user_image=")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

huangapple
  • 本文由 发表于 2022年6月17日 14:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/72654965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定