1

So, I'm new to web development I'm just trying to get the basics down. Any ideas of even where I can find hints to a solutions to this issue would help a lot.

When my client enters the url http://localhost:5000/creatAccountPage the server sends him a basic html page. Then directly the html page calls the javascript method getCaptachaImage() :

async function getCapatchaImage()
{
        const transmittingData =
        {
          clientIdi: "152412",
        };
        const options = 
         {
            method:'POST', 
            headers: {'Content-Type':'application/json'},
            body:JSON.stringify(transmittingData)
         };
        
        const response = await fetch('/getCaptchaImage',options);
        const jsonResponse = await response.json();
        document.getElementById("memory").innerHTML = jsonresponse.randomKey+"";
        document.getElementById("captcha_pic").src = jsonresponse.image; //image is not the right type for this to work. I think i need  to transform my json data to the right format.
}

the getCapatchaImage() method is suppose to get an image and the variable randomKey from the server. Below is my server code for the post request /getCaptachaImage I'm using Node.js, express and Node-Fetch.

//these are not all the requires but I'm just putting the essentials for this issue.
const fetch = require('node-fetch');
const express = require('express');
const app = express(); 

app.post("/getCaptchaImage",function(req, res)
{
var randomNumber = Math.random()* 100000;
var fullKey = randomNumber +process.env.CAPTCHA_SECRET+"";
const verifyUrl = `http://image.captchas.net?client=demo&random=`+fullKey; //I'm getting my image from an external server I can't get it directly from the client code else the client would know the fullkey which he is not suppose to know.
fetch(verifyUrl)
.then((body) => {
  return res.json({image: body.body, randomKey: randomNumber}); //I suppose I should probably be transforming my image in to a specific format here too. 
});
});

My general idea was to transmit my image together with my random key was just to put the image data in a string variable image of a json object together with the variable randomkey then find a away to translate the string image data in to the format I need for: document.getElementById("captcha_pic").src = ????

Any idea of how to transform my image data for this to work?

With there being no clear types for variables in javascript I'm having trouble understanding even what the format of my data is of "body" in the fetch function of my server code. All explanations in the Node-fetch api either I don't understand or they don't tell me what I need to know. https://www.npmjs.com/package//node-fetch#buffer

thanks in advance for any help.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
Cavern head
  • 21
  • 1
  • 7
  • What is the significance of Random key? – Apoorva Chikara Apr 19 '21 at 10:38
  • This https://stackoverflow.com/a/16449483/941240 – Wiktor Zychla Apr 19 '21 at 10:45
  • when the client finishes filling up his form the client sends back the random key to the server , so the server can recalculate the full key once again and check if the clients answer to what is written on the image was right but really i know the captcha system i'm doing is flawed i just want to master the data conversion situation as a general rule. seems like a super basic thing to just send mix data with images and other variables. – Cavern head Apr 19 '21 at 11:59
  • Wiktor Zychla so that's i guess is the answer to the end part of my problem that already helps a lot so thx. I guess i still need to figure out what part of the jsonresponse.image i should convert to base64 and how – Cavern head Apr 19 '21 at 12:17
  • @WiktorZychla Must say i'm really not sure what and how to convert tobase64 on top of it all when I try the setAttributes(src,base64convertedData) i get on the client the error: ** GET http://localhost:5000/'data:image/png;base64 404 (Not Found) ** so it seems to me like the server is suppose to serve the image with another method. – Cavern head Apr 19 '21 at 14:52

0 Answers0