Ruby: Net::HTTP Why do I receive a 400 for this POST request?

33 views Asked by At

I am trying out GPTZero's API. The docs.

The documentation says I need to set the api key in the header. I have left it as a blank string as the website says I "…can try out the API right here in the docs without an API-key for a limited number of uses."

I believe I have set document and version params correctly.

Here is my 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)

However the response is 400 Bad Request with the error message: "Text must be a string".

What is wrong with my request? Have I sent it correctly?

1

There are 1 answers

0
Pascal On

Looks like you are missing the Content-Type header. This example works for me:

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

It prints

{"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"}]}