react-nativegoogle-cloud-platformcurlgoogle-geminigoogle-generativeai

Error 400 invalid JSON Payload Unknown name generationConfig on an AI API Curl command


I'm trying to use Generative Language API (Gemini API) in my React Native so I'm first trying with a curl. Someone gave me this command :

`

API_KEY="YOUR_API_KEY"



# Adjust safety settings in generationConfig below.

# See https://ai.google.dev/gemini-api/docs/safety-settings

curl \

  -X POST https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY} \

  -H 'Content-Type: application/json' \

  -d @<(echo '{

  "contents": [

    {

      "role": "user",

      "parts": [

        {

          "text": "input: "

        },

        {

          "text": "output: "

        }

      ]

    }

  ],

  "generationConfig": {

    "temperature": 1,

    "topK": 64,

    "topP": 0.95,

    "maxOutputTokens": 8192,

    "responseMimeType": "text/plain"

  }

}')

`

But I got this error :

"error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \"generationConfig\": Cannot find field.", "status": "INVALID_ARGUMENT", "details": [ { "@type": "type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ { "description": "Invalid JSON payload received. Unknown name \"generationConfig\": Cannot find field." } ] } ] } }'

Is there something other than the API key to adjust ? I'm trying to use Generative Language API (Gemini API) in my React Native so I'm first trying with a curl. I'd like to have an response from the AI.


Solution

  • It would be easier and better to convert the сURL and use Axios or Fetch:

    Using Axios:

    npm install axios
    

    import axios from 'axios';
    
    const API_KEY = 'YOUR_API_KEY';
    const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY}`;
    
    const requestData = {
      contents: [
        {
          role: 'user',
          parts: [
            { text: 'input: ' },
            { text: 'output: ' }
          ]
        }
      ],
      generationConfig: {
        temperature: 1,
        topK: 64,
        topP: 0.95,
        maxOutputTokens: 8192,
        responseMimeType: 'text/plain'
      }
    };
    
    axios.post(url, requestData, {
      headers: {
        'Content-Type': 'application/json'
      }
    })
    .then(response => {
      console.log(response.data);
    })
    .catch(error => {
      console.error('Error:', error);
    });
    

    Using standard Fetch:

    const API_KEY = 'YOUR_API_KEY';
    const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY}`;
    
    const requestData = {
      contents: [
        {
          role: 'user',
          parts: [
            { text: 'input: ' },
            { text: 'output: ' }
          ]
        }
      ],
      generationConfig: {
        temperature: 1,
        topK: 64,
        topP: 0.95,
        maxOutputTokens: 8192,
        responseMimeType: 'text/plain'
      }
    };
    
    fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(requestData)
    })
      .then(response => response.json())
      .then(data => console.log(data))
      .catch(error => console.error('Error:', error));