'Http response returning two string value in Python

Hi I have two string in Python like

string1 = "['Rilassante piacere', 'Abbraccio vellutato', 'Dosha Vata']";
string2 = "[1, 1, 4]";

I have tried passing in the response

return func.HttpResponse(string1,string2, status_code=200)

But obiviusly it is not possible, should I encode them in JSON?

So my questions are:

  • How I can send this two string in HttpResponse in my Python script?

  • From the calling the URL 'https://example.com/response' how I can use this response in JS to retrieve these strings?

Thanks in advance



Solution 1:[1]

You can use the below code and see if it helps

return HttpResponse(json.dumps({"string1":string1, "string2":string1}), status_code=200)

you can add as many variables or data as you want inside the JSON object.

to make a request for pure javascript try the below solution

const Http = new XMLHttpRequest();
const url='https://jsonplaceholder.typicode.com/posts'; this url is the url of your python endpoint
Http.open("GET", url);
Http.setRequestHeader('Access-Control-Allow-Headers', '*');
Http.send();
console.log(Http.response)
response = Http.response // this response should be an object when you use your url
status1 = response['status1']
status2 = response['status2']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1