Ruby:Net::HTTP为什么这个POST请求会出现400?

7rtdyuoh  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(139)

我正在试用GPTZero的API。docs
文档中说我需要在头文件中设置API键。我把它作为一个空字符串留了下来,因为website说我“.
我相信我已经正确设置了documentversion参数。
下面是我的Ruby:

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,并带有错误消息:“Text must be a string”。
我的请求有什么问题?我发送的请求是否正确?

hyrbngr7

hyrbngr71#

看起来你缺少了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"}]}

相关问题