Ruby: Net::HTTP 为什么我会收到此 POST 请求的 400?

问题描述 投票:0回答:1

我正在尝试 GPTZero 的 API。 文档

文档说我需要在标头中设置 api 密钥。我将其保留为空白字符串,因为网站说我“……可以在文档中尝试该 API,而无需 API 密钥,但用途有限。”

我相信我已经正确设置了

document
version
参数。

这是我的红宝石:

base = "https://api.gptzero.me"
uri = URI("#{base}/v2/predict/text")

api_key = ""
headers = {
  "x-api-key" => api_key
}
params = {
  "document" => "How now, brown cow.",
  "version" => ""
}


res = Net::HTTP.post(uri, params.to_json, headers)

但是,响应是

400 Bad Request
,并显示错误消息:“文本必须是字符串”。

我的要求有什么问题吗?我发送正确了吗?

ruby net-http
1个回答
0
投票

看起来您缺少

Content-Type
标题。这个例子对我有用:

require "net/http"
require "json"
base = "https://api.gptzero.me"
uri = URI("#{base}/v2/predict/text")

api_key = ""
headers = {
  "x-api-key" => api_key,
  "Content-Type" =>  "application/json"
}
params = {
  "document" => "How now, brown cow.",
  "version" => "2023-06-30"
}

res = Net::HTTP.post(uri, params.to_json, headers)
puts res.body

打印出来了

{"version":"2023-06-30","documents":[{"average_generated_prob":0.0475141815841198,"completely_generated_prob":0.0475141815841198,"overall_burstiness":0,"paragraphs":[{"completely_generated_prob":0.11111110864197533,"num_sentences":1,"start_sentence_index":0}],"sentences":[{"generated_prob":0.0475141815841198,"perplexity":947,"sentence":"How now, brown cow.","highlight_sentence_for_ai":false}],"writing_stats":{},"result_message":"Your text is likely to be written entirely by a human","document_classification":"HUMAN_ONLY","version":"2023-06-30","document_id":"174f6d03-1cdf-4ed7-a1e3-99f23d76d0b2"}]}
© www.soinside.com 2019 - 2024. All rights reserved.