Skip to content

Forum in maintenance, we will back soon 🙂

I am getting 400 Ba...
 
Notifications
Clear all

I am getting 400 Bad Request when testing API using Thunder Client

7 Posts
2 Users
2 Reactions
10 Views
(@tejas-gandhi)
Posts: 4
Active Member Customer
Topic starter
 

Sir, I am facing this issue in your skillshare course. I have already mentioned details with screenshots inside Discussion tab. Please help me. I have created thread for more than 4 days now. I am not able to follow along because of this issue.

 

Kindly help.

 

Thanks in advance

 
Posted : 07/16/2024 2:50 pm
(@husein)
Posts: 550
Member Moderator
 

Hello @tejas-gandhi, can you give more details about your issue, where you are getting this problem in which course (course name), and which code, and share the code with us so we can help you?

 
Posted : 07/16/2024 4:05 pm
Tejas Gandhi reacted
(@tejas-gandhi)
Posts: 4
Active Member Customer
Topic starter
 

Sir, the course name is "How To Create AI Tools With WordPress" on Skillshare.

Here is the complete code:

function tg_openai_generate_text_basic() {

// Get the topic from the AJAX request
$topic = $_POST['topic'];
$usage = $_POST['usage'];
$prompt = "As an expert copywriter specialized in hook generation, your task is to Generate 3 hooks for the following topic: {" . $topic . "} for using it in a: {" . $usage . "}. The output should be a Only The list of Hooks with the type.

The Hooks List:\n\n";

// OpenAI API URL and key
$api_url = 'https://api.openai.com/v1/chat/completions';
$api_key = 'sk-*****'; // OpenAI API key

// Headers for the OpenAI API
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key
];

// Body for the OpenAI API
$body = [
'model' => 'gpt-3.5-turbo',
'messages' => [['role' => 'user', 'content' => $prompt]],
'temperature' => 0.7
];

// Args for the WordPress HTTP API
$args = [
'method' => 'POST',
'headers' => $headers,
'body' => json_encode($body),
'timeout' => 120
];

// Send the request
$response = wp_remote_request($api_url, $args);

// Handle the response
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
wp_send_json_error("Something went wrong: $error_message");
} else {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);

if (json_last_error() !== JSON_ERROR_NONE) {
wp_send_json_error('Invalid JSON in API response');
} elseif (!isset($data['choices']) || !isset($data['choices'][0]['message']['content'])) {
wp_send_json_error('API request failed. Response: ' . $body);
} else {
$content = $data['choices'][0]['message']['content'];

wp_send_json($content);
}
}

// Always die in functions echoing AJAX content
wp_die();
}

add_action('wp_ajax_openai_generate_text_basic', 'tg_openai_generate_text_basic');
add_action('wp_ajax_nopriv_openai_generate_text_basic', 'tg_openai_generate_text_basic');

 
Posted : 07/19/2024 3:16 am
(@husein)
Posts: 550
Member Moderator
 

@tejas-gandhi I just tested it from my end I just inserted my own API key and it worked. Did you replace the API key variable with your own?

 
Posted : 07/19/2024 8:03 am
(@tejas-gandhi)
Posts: 4
Active Member Customer
Topic starter
 

@husein sir, yes I did

 
Posted : 07/19/2024 11:56 am
(@tejas-gandhi)
Posts: 4
Active Member Customer
Topic starter
 

Sir,

Can you please check the following:

 

https://tejasgandhi.com/wp-admin/admin-ajax.php

action = tg_openai_generate_text_basic
topic = digital marketing
usage = instagram reels

 
Posted : 07/20/2024 4:09 am
(@husein)
Posts: 550
Member Moderator
 

@tejas-gandhi It worked.

 
Posted : 07/20/2024 10:24 am
Share:
[the_ad_group id="312"]